2015-02-24 46 views
0

我已经从此问题安装了一个插件 - > 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

我发现了一个“古老”的方式来做到这一点。 如果你搜索“商店”中的脚本”,在它的结束,你会发现串

update_option(‘recent_searches_widget_data’,$数据);

如果设置$数据为0(例如$ data = 0;),然后您尝试搜索某些内容,则可以删除搜索历史记录 请记住删除将$ data设置为0的行,否则插件不会再次开始工作

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

+0

嗨Tabris963,非常感谢你!有效。但是,现在我在同一个问题上面临着不同的问题。现在,我将搜索到的关键字数量设置为“4”。正确的,它应该只显示我的网站上4个搜索。但是,我不知道为什么无论我设置什么都不起作用。此问题已更新。你可以看看,以便知道哪一部分。再次感谢! – Jornes 2015-02-25 02:11:47

+0

我试图找到一种方式,但由于某种原因,当我应用更改别的东西(我无法控制)似乎应用于(例如,它开始以大写字母显示所有内容或它改变了当他想要的元素(这不是我正在寻找的解决方案,因为我不能“复制”它))... – tabris963 2015-02-25 10:24:21

+0

嗨Tabris963,谢谢!希望你能找到解决这个问题的方法。我真的很需要这个插件,只有三个类似的插件,这是最好的,因为其余的都是没有指示的。不知道如何配置和使用。你知道一个名为“最近的搜索”的插件吗?没有说明如何配置。你知道吗? :) – Jornes 2015-02-25 11:02:03