我正在为一个朋友创建一个插件,用于在他的网站上记录搜索词。这是非常基本的。它创建一个新表来存储在搜索框中输入的术语。它将搜索项转换为小写,检查数据库中是否有匹配项,并将该项插入数据库或增加匹配项的计数。它在我的托管帐户上按预期工作。然后我在GoDaddy托管帐户上的WP安装上安装了它。。。
只要我登录,无论数据库中是否有匹配的术语,似乎都能正常工作。当我以访客身份搜索网站时,它会向数据库中添加一个新的唯一搜索词,但如果存在现有词,则不会执行任何操作。嗯,这不完全是真的。我已经看到它增加了数倍,但不是1倍,而是2倍。它似乎偶尔在戈达迪身上发挥作用。我搞不懂这个。可以找到实际的插件here.
我很感激花时间看这个。
谢谢
// Register function to be called when plugin is activated
register_activation_hook( __FILE__, \'mac_search_log_activation\' );
function mac_search_log_activation(){
global $wpdb;
$prefix = $wpdb->prefix;
$creation_query =
\'CREATE TABLE \' . $prefix . \'mac_search_log (
`id` mediumint(8) NOT NULL AUTO_INCREMENT,
`full_search_term` text NOT NULL,
`count` mediumint(8) NOT NULL,
PRIMARY KEY (`id`)
);\';
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
dbDelta( $creation_query );
}
// Register function to be called when administration pages init takes place
add_action( \'admin_init\', \'mac_search_log_admin_init\' );
function mac_search_log_admin_init(){
add_action(\'admin_post_delete_mac_search_log_term\', \'delete_mac_search_log_term\');
}
// Register function to be called when admin menu is constructed
add_action( \'admin_menu\', \'mac_search_log_settings_menu\' );
// Add new menu item for Search Term History
function mac_search_log_settings_menu() {
add_menu_page( \'Search Term History\',
\'Search Term History\', \'manage_options\',
\'search-term-history\',
\'mac_search_log_config_page\',
null,
4.7);
}
// Function to render plugin admin page
function mac_search_log_config_page() {
global $wpdb;
?>
<!-- Top-level menu -->
<div id="mac_search_log-general" class="wrap">
<h2>Searched Terms</h2>
<form method="post"
action="<?php echo admin_url( \'admin-post.php\' ); ?>">
<input type="hidden" name="action" value="delete_mac_search_log_term" />
<?php wp_nonce_field( \'mac_search_log_term_deletion\' ); ?>
<?php
$term_query = \'select * from \';
$term_query .= $wpdb->prefix;
$term_query .= \'mac_search_log ORDER by count DESC\';
$term_items = $wpdb->get_results( $wpdb->prepare( $term_query ), ARRAY_A );
?>
<table class="wp-list-table widefat fixed" >
<thead><tr>
<th style="width: 50px">Delete</th><th style="width: 300px">Search Term</th><th>Count</th></tr></thead>
<?php
// Display search terms if query returned results
if ( $term_items ) {
foreach ( $term_items as $term_item ) {
echo \'<tr style="background: #FFF">\';
echo \'<td><input type="checkbox" name="terms[]" value="\';
echo esc_attr( $term_item[\'id\'] ) . \'" /></td>\';
echo \'<td>\' . $term_item[\'full_search_term\'] . \'</td>\';;
echo \'<td>\' . $term_item[\'count\'] . \'</td></tr>\';
}
} else {
echo \'<tr style="background: #FFF">\';
echo \'<td colspan=4>No Searches Yet</td></tr>\';
}
?>
</table><br />
<input type="submit" value="Delete Selected"
class="button-primary"/>
</form>
</div>
<?php }
add_filter(\'pre_get_posts\',\'MACSearchLog\');
function MACSearchLog($query) {
if ($query->is_search) {
global $wpdb;
$term = strtolower($query->query_vars[\'s\']);
$term_query = \'select * from \';
$term_query .= $wpdb->prefix;
$term_query .= \'mac_search_log\';
$term_query .= \' WHERE full_search_term = "\'.$term.\'"\';
$term_items = $wpdb->get_row( $wpdb->prepare( $term_query ), ARRAY_A );
if(empty($term_items)){
// insert term and set count to 1
$term_item = array(
\'full_search_term\' => $term,
\'count\' => 1
);
$wpdb->insert($wpdb->prefix.\'mac_search_log\', $term_item);
}else{
// update existing row count++
$new_query = \'UPDATE \';
$new_query .= $wpdb->prefix;
$new_query .= \'mac_search_log\';
$new_query .= \' SET count = count+1 \';
$new_query .= \'WHERE id = \'.$term_items[\'id\'];
$wpdb->query($wpdb->prepare($new_query));
}
}
}
function delete_mac_search_log_term(){
$deleted_terms = array();
// Check that user has proper security level
if ( !current_user_can( \'manage_options\' ) )
wp_die( \'Not allowed\' );
// Check if nonce field is present
check_admin_referer( \'mac_search_log_term_deletion\' );
// If search terms are present, cycle through array and call SQL
// command to delete entries one by one
if ( !empty( $_POST[\'terms\'] ) ) {
// Retrieve array of search term IDs to be deleted
$terms_to_delete = $_POST[\'terms\'];
global $wpdb;
foreach ( $terms_to_delete as $term_to_delete ) {
$deleted_terms[] = get_search_term_by_id(intval($term_to_delete));// TO GET SEARCH TERMS FOR EMAIL
$query = \'DELETE from \' . $wpdb->prefix;
$query .= \'mac_search_log \';
$query .= \'WHERE id = \' .
intval( $term_to_delete );
$wpdb->query( $wpdb->prepare( $query ) );
}
}
// UNCOMMENT THE FOLLOWING 2 LINES TO DISPATCH EMAIL WITH DELETED SEARCH TERMS
// $terms = print_r($deleted_terms, true);
// wp_mail( get_option(\'admin_email\'), \'Search Terms Deleted\', $terms);
// Reload the search term history page
wp_redirect( add_query_arg( \'page\', \'search-term-history\',
admin_url( \'admin.php\' ) ) );
exit;
}
function get_search_term_by_id($id){
global $wpdb;
$term_query = \'select * from \';
$term_query .= $wpdb->prefix;
$term_query .= \'mac_search_log \';
$term_query .= \'WHERE `id` = \'.$id;
$term_item = $wpdb->get_row( $wpdb->prepare( $term_query ), OBJECT );
if($term_item){
return $term_item->full_search_term;
}else{
return null;
}
}
编辑:我一直在记录查询和其他相关内容。什么都看不见。我用$wpdb->update()替换了SQL查询,并替换了
if(empty($term_items)){
具有
if($term_items)
在交换相关操作时,这似乎已经清除了一些计数异常,但除非我登录,否则它不会在GoDaddy帐户上更新。一方面,我很难相信一个不同的托管帐户会对这一点产生影响,但过去我在GoDaddy看到过一些奇怪的事情。
最合适的回答,由SO网友:Kurt Payne 整理而成
我能够重新创建此缓存,但在我的配置中,绕过您的查询的是CDN(边缘缓存)和/或浏览器缓存。
要使浏览器缓存成为问题,必须通过单击链接进行搜索。例如,如果您单击了指向的链接?s=术语,它将首先检查浏览器缓存。如果这是表单GET的结果,那么它应该绕过浏览器缓存,但我可能错了。
To test this: 检查您的。用于缓存规则的htaccess文件。Go Daddy使用浏览器缓存规则来提高性能和与CDN的兼容性。
要使CDN/边缘缓存成为问题,您必须是访问者(无Cookie)。CDN缓存页面以及元信息(即页面如何“变化”),这通常是用户代理、语言和其他一些内容。
Go Daddy的最终托管计划附带了CDN功能(这是我测试的),但cloudflare或其他CDN也可能会出现这种情况。
To test this: 在其他浏览器中访问您的站点,或修改主机文件,以使用主机服务器的IP地址而不是CDN的IP地址。
对于解决方案,我个人建议使用ajax。您不能阻止某个层缓存搜索页面的呈现HTML输出,但可以嵌入ajax回调来记录搜索字符串。
我希望这有帮助!