【问题标题】:Wordpress, add_filter, get_search_formWordpress、add_filter、get_search_form
【发布时间】:2012-02-24 18:18:32
【问题描述】:

我正在尝试用我自己的方式替换搜索表单。

一个示例插件:

<?php
/*
Author: whatever
Plugin Name: Some Name
Plugin URI: 
Text Domain: some-domain
Version: 1.0
*/

function custom_search($form)
{
$form = "testing<form method = 'get' id = 'searchform' action = ' ".site_url()." ' >";
$form .= "<div><label class = 'hidden' for='s'>". __("Search for: ") . "</label>";
$form .= "<input type = 'text' value=' ". esc_attr(apply_filters('the_search_query', get_search_query())) ." ' name='s' id='s' />";
$form .= "<input type = 'submit' id='searchsubmit' value=' ".esc_attr(__('Look for it'))." ' />";
$form .= "</div>";
$form .= "</form>";

return $form;
}

add_filter('get_search_form', 'custom_search');

结果,不起作用。

我正在使用带有 211 主题的最新 Wordpress。

我在这里错过了什么?!?

编辑:如果主题有 searchform.php 文件,则该过滤器将被该文件覆盖。所以基本上过滤器只有在主题没有 searchform.php 时才有效。现在这只是愚蠢..

【问题讨论】:

    标签: wordpress add-filter


    【解决方案1】:

    这是由于get_search_form( $echo ) 函数中的一个众所周知的 WordPress 错误将忽略 $echo 参数并始终回显您的主题中有一个 searchform.php 模板文件。

    这是一个垃圾函数,有一个 2 岁的 trac ticket。在他们修复它之前,几乎没有解决方法: 1. 使用file_get_contents() - 但不会解析PHP 2. 使用带有 get_searchform 钩子的自定义函数(不是实际函数) 3.或者使用简单的输出缓冲将输出捕获为变量;

    This page has examples for all 3 methods

    【讨论】:

    • “此页面”是您自己的页面,当您发布指向您自己的页面/网站/产品的链接时,您必须披露这一事实。
    猜你喜欢
    • 2016-12-12
    • 1970-01-01
    • 2014-06-04
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多