phpwordpresssearchplugins

How to clear the recent searched keywords display on website - Recent Searches Widget


I have installed a plugin from this question --> Show most recent search terms in wordpress. I have the plugin working on my site, but i would like to ask that, how to clear the recent searched keywords that displaying on my website. I am running out of idea how to make it happen.

As you can see the result from my site: www.ncc.my. There are now 10 keywords on top of search box. I want to clear all the keyword. I have searched over the google but yet to get the answer. Here is the php code of the widget. See if you can get the solution to clear the keywords. Thanks!

    <?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

?>

I can't see which part is for clearing up the keywords. Any suggestion? Thanks!

Updated part: After the clearing history issue solved, this is the next issue i got.

function get_default_options() {
        return array(
            'title' => '',
            'max' => 4,  <---it was originally set to 10
            'nofollow' => true,
        );
    }
}

I have set the searched keywords to "4" which was originally set as 10, it should work and display only maximum 4 keywords by right. But, i don't know why the setting seems to follow the very first time i use this plugin. No matter how i tried to set from 0 to 5, the setting never take effect and the keywords there are still displaying as maximum 10 searches. Need help in this!

Any solution?


Solution

  • I found an "archaic" way to do it. If you search for "store" in the script", at the end of it you'll find the string

    update_option( 'recent_searches_widget_data', $data );

    If you set $data to 0 (e.g. $data=0;)right before this line and then you try to search something, you'll be able to delete the search history. Remember to delete the line where you set $data to 0, otherwise the plugin won't start to work again

    Edit: Sorry for the late response, but I have the answers for your question (and also another way to do the previous point). What I understood is that from the script you can't change after the initialisation the number of elements that have to be shown. When does this happen? When you install the plugin. So you should unzip the folder, change the number of elements in the .php file, zip the folder and install again the plugin. Not so tricky, but quite long. Now, the good news. I don't know if you have ever managed with the database that is behind wp, but there is a table (wp_options) where many part of code that wordpress uses are stored. Usually here you can also find the data that the plugins set up. And here we go: in that table there are two rows, called recent_searches_widget and recent_searches_widget_data. In the first you can find the setting for the number of elements to be shown (its the i:10; that is set by default, if you haven't changed the script yet), in the other you can find the previous searches (and, if you want to delete them, you just need to change the value of the row to 0).