【问题标题】:How to clear the recent searched keywords display on website - Recent Searches Widget如何清除网站上最近搜索的关键字显示 - 最近搜索小部件
【发布时间】:2015-02-24 17:52:13
【问题描述】:

我已经安装了这个问题的插件 --> 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 次搜索。在这方面需要帮助!

有什么办法吗?

【问题讨论】:

    标签: search plugins


    【解决方案1】:

    我找到了一种“古老”的方法。 如果您在脚本中搜索“store”,则会在其末尾找到字符串

    update_option('recent_searches_widget_data', $data);

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

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

    【讨论】:

    • 您好 Tabris963,非常感谢您!有效。但是,现在我在同一个问题上面临不同的问题。现在,我将搜索关键字的数量设置为“4”。正确地,它应该在我的网站上只显示 4 次搜索。但是,我不知道为什么无论我设置什么它都不起作用。此问题已更新。你可以看看,这样你就知道是哪个部分了。再次感谢!
    • 我正在尝试找到一种方法,但由于某种原因,当我应用更改时,似乎应用了其他东西(我无法控制)(例如,它开始以大写形式显示所有内容字母或者它在他想要的时候改变元素的数量(这不是我正在寻找的解决方案,因为我不能“复制”它))......
    • 您好 Tabris963,谢谢!希望你能找到解决这个问题的方法。我真的非常需要这个插件,只有三个类似的插件,这是最好的一个,因为其余的都是在没有说明的情况下发布的。不知道怎么配置和使用。您知道仅名为“最近搜索”的插件吗?没有说明如何配置。你知道吗? :)
    • 我编辑了我以前的帖子,应该有你正在寻找的答案;)(如果我错过了一些解释,不要害怕问!)但是不,我不知道那些插件,我真的不需要在我的 wordpress 上使用它们:P
    • 您好 Tabris963,非常感谢您的努力!我尝试了您在更新帖子中提到的方法,即在上传和安装插件之前更改要显示的元素数量。但是,它没有用。自从我第一次安装这个插件以来就发生了这个问题。通常我从 wordpress 仪表板的插件页面安装插件。而且,我想从你那里得到指导,如何管理wordpress的数据库?在 wordpress 中有很多东西要学。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-04-28
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多