如何清除网站上最近搜索的关键字显示 - 最近搜索小部件

时间:2015-02-24 17:52:14

标签: search plugins

我已经从这个问题安装了一个插件 - > Show most recent search terms in wordpress。我有插件在我的网站上工作,但我想问一下,如何清除我网站上显示的最近搜索的关键字。我已经不知道如何实现它。

您可以在我的网站上看到结果:www.ncc.my。搜索框顶部有10个关键字。我想清除所有关键字。我搜索了谷歌但尚未得到答案。这是小部件的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 = '&nbsp;';
        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”,最初设置为10,它应该可以正常显示最多4个关键字。但是,我不知道为什么设置似乎跟随我第一次使用这个插件。无论我如何设置从0到5,设置永远不会生效,其中的关键字仍然显示为最多10次搜索。需要帮助!

任何解决方案?

1 个答案:

答案 0 :(得分:1)

我找到了一个古老的&#34;这样做的方式。 如果您搜索&#34; store&#34;在脚本&#34;中,在它的末尾你会找到字符串

  

update_option(&#39; recent_searches_widget_data&#39;,$ data);

如果您在此行之前将$ data设置为0(例如$ data = 0;),然后尝试搜索某些内容,则您将能够删除搜索历史记录。 请记住删除将$ data设置为0的行,否则插件将无法再次开始工作

修改: 对于迟到的回复感到抱歉,但我有你的问题的答案(以及前一点的另一种方法)。 我所理解的是,从初始化后你可以改变必须显示的元素数量。这是什么时候发生的?安装插件时。所以你应该解压缩文件夹,更改.php文件中的元素数量,压缩文件夹并再次安装插件。 不是那么棘手,但很长。 现在,好消息。 我不知道你是否曾经使用wp背后的数据库进行管理,但是有一个表( wp_options ),其中存储了wordpress使用的许多代码部分。 通常在这里您还可以找到插件设置的数据。 我们走了:在该表中有两行,称为 recent_searches_widget recent_searches_widget_data 。 在第一个中,您可以找到要显示的元素数量的设置(默认情况下设置的 i:10; ,如果您尚未更改脚本),另一个你可以找到以前的搜索(如果你想删除它们,你只需要将行的值更改为 0 )。