如何清除网站上最近搜索的关键字显示 - 最近搜索小部件
How to clear the recent searched keywords display on website - Recent Searches Widget
我已经安装了这个问题的插件 -->
Show most recent search terms in wordpress。我的网站上有插件,但我想问一下,如何清除我网站上显示的最近搜索的关键字。我 运行 不知道如何实现它。
正如您在我的网站上看到的结果:www.ncc.my。搜索框顶部现在有 10 个关键字。我想清除所有关键字。我搜索了 google 但还没有得到答案。这是小部件的 php 代码。看看你能不能得到清除关键字的解决方案。谢谢!
<?php
/*
Plugin Name: Recent Searches Widget
Plugin URI: http://www.poradnik-webmastera.com/projekty/recent_searches_widget/
Description: Shows recent searches in a sidebar widget.
Author: Daniel Frużyński
Version: 1.2
Author URI: http://www.poradnik-webmastera.com/
Text Domain: recent-searches-widget
*/
/* Copyright 2009-2010 Daniel Frużyński (email : daniel [A-T] poradnik-webmastera.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( !class_exists( 'RecentSearchesWidget' ) ) {
class RecentSearchesWidget {
// Constructor
function RecentSearchesWidget() {
// Initialize plugin
add_action( 'init', array( &$this, 'init' ) );
// Page load
add_action( 'template_redirect', array( &$this, 'template_redirect' ) );
// Widgets initialization
add_action( 'widgets_init', array( &$this, 'widgets_init' ) );
}
// Plugin initialization
function init() {
load_plugin_textdomain( 'recent-searches-widget', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
}
// Page load
function template_redirect() {
if ( is_search() ) {
// Store search term
$query = $this->strtolower( trim( get_search_query() ) );
$options = get_option( 'recent_searches_widget' );
if ( !is_array( $options ) ) {
$options = $this->get_default_options();
}
$max = $options['max'];
$data = get_option( 'recent_searches_widget_data', array() );
if ( !is_array( $data ) ) {
if ( isset( $options['data'] ) ) {
$data = $options['data'];
unset( $options['data'] );
update_option( 'recent_searches_widget', $options );
}
if ( !is_array( $data ) ) {
$data = array();
}
}
$pos = array_search( $query, $data );
if ( $pos !== false ) {
if ( $pos != 0 ) {
$data = array_merge( array_slice( $data, 0, $pos ),
array( $query ), array_slice( $data, $pos + 1 ) );
}
} else {
array_unshift( $data, $query );
if ( count( $data ) > $max ) {
array_pop( $data );
}
}
update_option( 'recent_searches_widget_data', $data );
}
}
// Widgets initialization
function widgets_init() {
$widget_ops = array(
'classname' => 'widget_rsw',
'description' => __('Shows recent searches', 'recent-searches-widget'),
);
wp_register_sidebar_widget( 'recentsearcheswidget', __('Recent Searches', 'recent-searches-widget'),
array( &$this, 'widget_rsw' ), $widget_ops );
wp_register_widget_control( 'recentsearcheswidget', __('Recent Searches', 'recent-searches-widget'),
array( &$this, 'widget_rsw_control' ) );
}
function widget_rsw( $args ) {
extract( $args );
$title = isset( $options['title'] ) ? $options['title'] : '';
$title = apply_filters( 'widget_title', $title );
if ( empty($title) )
$title = ' ';
echo $before_widget . $before_title . $title . $after_title, "\n";
$this->show_recent_searches( "<ul>\n<li>", "</li>\n</ul>", "</li>\n<li>" );
echo $after_widget;
}
function show_recent_searches( $before_list, $after_list, $between_items ) {
$options = get_option( 'recent_searches_widget' );
if ( !is_array( $options ) ) {
$options = $this->get_default_options();
}
$data = get_option( 'recent_searches_widget_data' );
if ( !is_array( $data ) ) {
if ( isset( $options['data'] ) ) {
$data = $options['data'];
}
if ( !is_array( $data ) ) {
$data = array();
}
}
if ( count( $data ) > 0 ) {
echo $before_list;
$first = true;
foreach ( $data as $search ) {
if ( $first ) {
$first = false;
} else {
echo $between_items;
}
echo '<a href="', get_search_link( $search ), '"';
if ( $options['nofollow'] ) {
echo ' rel="nofollow"';
}
echo '>', wp_specialchars( $search ), '</a>';
}
echo $after_list, "\n";
} else {
_e('No searches yet', 'recent-searches-widget');
}
}
function widget_rsw_control() {
$options = $newoptions = get_option('recent_searches_widget', array() );
if ( count( $options ) == 0 ) {
$options = $this->get_default_options();
update_option( 'recent_searches_widget', $options );
}
if ( isset( $_POST['rsw-submit'] ) ) {
$options['title'] = strip_tags( stripslashes( $_POST['rsw-title'] ) );
$options['max'] = (int)( $_POST['rsw-max'] );
$options['nofollow'] = isset( $_POST['rsw-nofollow'] );
if ( count( $options['data'] ) > $options['max'] ) {
$options['data'] = array_slice( $options['data'], 0, $options['max'] );
}
update_option( 'recent_searches_widget', $options );
}
$title = attribute_escape( $options['title'] );
$max = attribute_escape( $options['max'] );
$nofollow = $options['nofollow'];
?>
<p><label for="rsw-title"><?php _e('Title:', 'recent-searches-widget'); ?> <input class="widefat" id="rsw-title" name="rsw-title" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="rsw-max"><?php _e('Max searches:', 'recent-searches-widget'); ?> <input id="rsw-max" name="rsw-max" type="text" size="3" maxlength="5" value="<?php echo $max; ?>" /></label></p>
<p><label for="rsw-nofollow"><?php _e('Add <code>rel="nofollow"</code> to links:', 'recent-searches-widget'); ?> <input id="rsw-nofollow" name="rsw-nofollow" type="checkbox" value="yes" <?php checked( $nofollow, true ); ?>" /></label></p>
<input type="hidden" id="rsw-submit" name="rsw-submit" value="1" />
<?php
}
// Make string lowercase
function strtolower( $str ) {
if ( function_exists( 'mb_strtolower' ) ) {
return mb_strtolower( $str );
} else {
return strtolower( $str );
}
}
function get_default_options() {
return array(
'title' => '',
'max' => 4,
'nofollow' => true,
);
}
}
// Add functions from WP2.8 for previous WP versions
if ( !function_exists( 'esc_html' ) ) {
function esc_html( $text ) {
return wp_specialchars( $text );
}
}
if ( !function_exists( 'esc_attr' ) ) {
function esc_attr( $text ) {
return attribute_escape( $text );
}
}
// Add functions from WP3.0 for previous WP versions
if ( !function_exists( 'get_search_link' ) ) {
function get_search_link( $query = '' ) {
global $wp_rewrite;
if ( empty($query) )
$search = get_search_query();
else
$search = stripslashes($query);
$permastruct = $wp_rewrite->get_search_permastruct();
if ( empty( $permastruct ) ) {
$link = home_url('?s=' . urlencode($search) );
} else {
$search = urlencode($search);
$search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
$link = str_replace( '%search%', $search, $permastruct );
$link = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $link, 'search' );
}
return apply_filters( 'search_link', $link, $search );
}
}
$wp_recent_searches_widget = new RecentSearchesWidget();
// Show recent searches anywhere in the theme
function rsw_show_recent_searches( $before_list = "<ul>\n<li>", $after_list = "</li>\n</ul>", $between_items = "</li>\n<li>" ) {
global $wp_recent_searches_widget;
$wp_recent_searches_widget->show_recent_searches( $before_list, $after_list, $between_items );
}
} // END
?>
看不出哪一部分是清理关键词的。有什么建议吗?谢谢!
更新部分:
清除历史问题解决后,这是我遇到的下一个问题
function get_default_options() {
return array(
'title' => '',
'max' => 4, <---it was originally set to 10
'nofollow' => true,
);
}
}
我已经将搜索关键字设置为“4”,原来设置为“4”,应该可以正常显示最多4个关键字。但是,我不知道为什么设置似乎遵循我第一次使用这个插件。无论我如何尝试设置从 0 到 5,该设置永远不会生效,那里的关键字仍然显示为最多 10 次搜索。在这方面需要帮助!
有什么解决办法吗?
我找到了 "archaic" 的方法。
如果您在脚本中搜索 "store"”,您会在脚本的末尾找到字符串
update_option( 'recent_searches_widget_data', $data );
如果您在此行之前将 $data 设置为 0(例如 $data=0;),然后尝试搜索某些内容,您将能够删除搜索历史记录。
记得把$data设置为0的那一行删掉,否则插件不会再开始工作了
编辑:
很抱歉回复晚了,但我有你问题的答案(还有另一种方法来做前一点)。
我的理解是,在初始化之后,您无法根据脚本更改必须显示的元素数量。这是什么时候发生的?安装插件时。所以你应该解压缩文件夹,更改 .php 文件中的元素数量,压缩文件夹并再次安装插件。
不是那么棘手,但相当长。
现在,好消息。
我不知道你是否曾经管理过 wp 后面的数据库,但是有一个 table (wp_options)存储 wordpress 用途。
通常在这里你也可以找到插件设置的数据。
我们开始吧:在 table 中有两行,称为 recent_searches_widget 和 recent_searches_widget_data .
在第一个中,您可以找到要显示的元素数量的设置(如果您尚未更改脚本,则默认设置为 i:10;),在另一个中你可以找到以前的搜索(如果你想删除它们,你只需要将行的值更改为 0)。
我已经安装了这个问题的插件 --> Show most recent search terms in wordpress。我的网站上有插件,但我想问一下,如何清除我网站上显示的最近搜索的关键字。我 运行 不知道如何实现它。
正如您在我的网站上看到的结果:www.ncc.my。搜索框顶部现在有 10 个关键字。我想清除所有关键字。我搜索了 google 但还没有得到答案。这是小部件的 php 代码。看看你能不能得到清除关键字的解决方案。谢谢!
<?php
/*
Plugin Name: Recent Searches Widget
Plugin URI: http://www.poradnik-webmastera.com/projekty/recent_searches_widget/
Description: Shows recent searches in a sidebar widget.
Author: Daniel Frużyński
Version: 1.2
Author URI: http://www.poradnik-webmastera.com/
Text Domain: recent-searches-widget
*/
/* Copyright 2009-2010 Daniel Frużyński (email : daniel [A-T] poradnik-webmastera.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( !class_exists( 'RecentSearchesWidget' ) ) {
class RecentSearchesWidget {
// Constructor
function RecentSearchesWidget() {
// Initialize plugin
add_action( 'init', array( &$this, 'init' ) );
// Page load
add_action( 'template_redirect', array( &$this, 'template_redirect' ) );
// Widgets initialization
add_action( 'widgets_init', array( &$this, 'widgets_init' ) );
}
// Plugin initialization
function init() {
load_plugin_textdomain( 'recent-searches-widget', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
}
// Page load
function template_redirect() {
if ( is_search() ) {
// Store search term
$query = $this->strtolower( trim( get_search_query() ) );
$options = get_option( 'recent_searches_widget' );
if ( !is_array( $options ) ) {
$options = $this->get_default_options();
}
$max = $options['max'];
$data = get_option( 'recent_searches_widget_data', array() );
if ( !is_array( $data ) ) {
if ( isset( $options['data'] ) ) {
$data = $options['data'];
unset( $options['data'] );
update_option( 'recent_searches_widget', $options );
}
if ( !is_array( $data ) ) {
$data = array();
}
}
$pos = array_search( $query, $data );
if ( $pos !== false ) {
if ( $pos != 0 ) {
$data = array_merge( array_slice( $data, 0, $pos ),
array( $query ), array_slice( $data, $pos + 1 ) );
}
} else {
array_unshift( $data, $query );
if ( count( $data ) > $max ) {
array_pop( $data );
}
}
update_option( 'recent_searches_widget_data', $data );
}
}
// Widgets initialization
function widgets_init() {
$widget_ops = array(
'classname' => 'widget_rsw',
'description' => __('Shows recent searches', 'recent-searches-widget'),
);
wp_register_sidebar_widget( 'recentsearcheswidget', __('Recent Searches', 'recent-searches-widget'),
array( &$this, 'widget_rsw' ), $widget_ops );
wp_register_widget_control( 'recentsearcheswidget', __('Recent Searches', 'recent-searches-widget'),
array( &$this, 'widget_rsw_control' ) );
}
function widget_rsw( $args ) {
extract( $args );
$title = isset( $options['title'] ) ? $options['title'] : '';
$title = apply_filters( 'widget_title', $title );
if ( empty($title) )
$title = ' ';
echo $before_widget . $before_title . $title . $after_title, "\n";
$this->show_recent_searches( "<ul>\n<li>", "</li>\n</ul>", "</li>\n<li>" );
echo $after_widget;
}
function show_recent_searches( $before_list, $after_list, $between_items ) {
$options = get_option( 'recent_searches_widget' );
if ( !is_array( $options ) ) {
$options = $this->get_default_options();
}
$data = get_option( 'recent_searches_widget_data' );
if ( !is_array( $data ) ) {
if ( isset( $options['data'] ) ) {
$data = $options['data'];
}
if ( !is_array( $data ) ) {
$data = array();
}
}
if ( count( $data ) > 0 ) {
echo $before_list;
$first = true;
foreach ( $data as $search ) {
if ( $first ) {
$first = false;
} else {
echo $between_items;
}
echo '<a href="', get_search_link( $search ), '"';
if ( $options['nofollow'] ) {
echo ' rel="nofollow"';
}
echo '>', wp_specialchars( $search ), '</a>';
}
echo $after_list, "\n";
} else {
_e('No searches yet', 'recent-searches-widget');
}
}
function widget_rsw_control() {
$options = $newoptions = get_option('recent_searches_widget', array() );
if ( count( $options ) == 0 ) {
$options = $this->get_default_options();
update_option( 'recent_searches_widget', $options );
}
if ( isset( $_POST['rsw-submit'] ) ) {
$options['title'] = strip_tags( stripslashes( $_POST['rsw-title'] ) );
$options['max'] = (int)( $_POST['rsw-max'] );
$options['nofollow'] = isset( $_POST['rsw-nofollow'] );
if ( count( $options['data'] ) > $options['max'] ) {
$options['data'] = array_slice( $options['data'], 0, $options['max'] );
}
update_option( 'recent_searches_widget', $options );
}
$title = attribute_escape( $options['title'] );
$max = attribute_escape( $options['max'] );
$nofollow = $options['nofollow'];
?>
<p><label for="rsw-title"><?php _e('Title:', 'recent-searches-widget'); ?> <input class="widefat" id="rsw-title" name="rsw-title" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="rsw-max"><?php _e('Max searches:', 'recent-searches-widget'); ?> <input id="rsw-max" name="rsw-max" type="text" size="3" maxlength="5" value="<?php echo $max; ?>" /></label></p>
<p><label for="rsw-nofollow"><?php _e('Add <code>rel="nofollow"</code> to links:', 'recent-searches-widget'); ?> <input id="rsw-nofollow" name="rsw-nofollow" type="checkbox" value="yes" <?php checked( $nofollow, true ); ?>" /></label></p>
<input type="hidden" id="rsw-submit" name="rsw-submit" value="1" />
<?php
}
// Make string lowercase
function strtolower( $str ) {
if ( function_exists( 'mb_strtolower' ) ) {
return mb_strtolower( $str );
} else {
return strtolower( $str );
}
}
function get_default_options() {
return array(
'title' => '',
'max' => 4,
'nofollow' => true,
);
}
}
// Add functions from WP2.8 for previous WP versions
if ( !function_exists( 'esc_html' ) ) {
function esc_html( $text ) {
return wp_specialchars( $text );
}
}
if ( !function_exists( 'esc_attr' ) ) {
function esc_attr( $text ) {
return attribute_escape( $text );
}
}
// Add functions from WP3.0 for previous WP versions
if ( !function_exists( 'get_search_link' ) ) {
function get_search_link( $query = '' ) {
global $wp_rewrite;
if ( empty($query) )
$search = get_search_query();
else
$search = stripslashes($query);
$permastruct = $wp_rewrite->get_search_permastruct();
if ( empty( $permastruct ) ) {
$link = home_url('?s=' . urlencode($search) );
} else {
$search = urlencode($search);
$search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
$link = str_replace( '%search%', $search, $permastruct );
$link = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $link, 'search' );
}
return apply_filters( 'search_link', $link, $search );
}
}
$wp_recent_searches_widget = new RecentSearchesWidget();
// Show recent searches anywhere in the theme
function rsw_show_recent_searches( $before_list = "<ul>\n<li>", $after_list = "</li>\n</ul>", $between_items = "</li>\n<li>" ) {
global $wp_recent_searches_widget;
$wp_recent_searches_widget->show_recent_searches( $before_list, $after_list, $between_items );
}
} // END
?>
看不出哪一部分是清理关键词的。有什么建议吗?谢谢!
更新部分: 清除历史问题解决后,这是我遇到的下一个问题
function get_default_options() {
return array(
'title' => '',
'max' => 4, <---it was originally set to 10
'nofollow' => true,
);
}
}
我已经将搜索关键字设置为“4”,原来设置为“4”,应该可以正常显示最多4个关键字。但是,我不知道为什么设置似乎遵循我第一次使用这个插件。无论我如何尝试设置从 0 到 5,该设置永远不会生效,那里的关键字仍然显示为最多 10 次搜索。在这方面需要帮助!
有什么解决办法吗?
我找到了 "archaic" 的方法。 如果您在脚本中搜索 "store"”,您会在脚本的末尾找到字符串
update_option( 'recent_searches_widget_data', $data );
如果您在此行之前将 $data 设置为 0(例如 $data=0;),然后尝试搜索某些内容,您将能够删除搜索历史记录。 记得把$data设置为0的那一行删掉,否则插件不会再开始工作了
编辑: 很抱歉回复晚了,但我有你问题的答案(还有另一种方法来做前一点)。 我的理解是,在初始化之后,您无法根据脚本更改必须显示的元素数量。这是什么时候发生的?安装插件时。所以你应该解压缩文件夹,更改 .php 文件中的元素数量,压缩文件夹并再次安装插件。 不是那么棘手,但相当长。 现在,好消息。 我不知道你是否曾经管理过 wp 后面的数据库,但是有一个 table (wp_options)存储 wordpress 用途。 通常在这里你也可以找到插件设置的数据。 我们开始吧:在 table 中有两行,称为 recent_searches_widget 和 recent_searches_widget_data . 在第一个中,您可以找到要显示的元素数量的设置(如果您尚未更改脚本,则默认设置为 i:10;),在另一个中你可以找到以前的搜索(如果你想删除它们,你只需要将行的值更改为 0)。