WordPress、add_filter、get_search_form [英] Wordpress, add_filter, get_search_form

查看:12
本文介绍了WordPress、add_filter、get_search_form的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替换我自己的搜索表单.

示例插件:

结果,不起作用.

我正在使用具有 211 个主题的最新 Wordpress.

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

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

解决方案

这是由 get_search_form( $echo ) 函数中众所周知的 WordPress 错误导致的,该错误将忽略 $echo 参数并始终echo jf 你的主题中有一个 searchform.php 模板文件.

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

该页面有所有 3 种方法的示例

I'm trying to replace the search form by my own.

An example plugin:

<?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');

Result, doesn't work.

I'm using the latest Wordpress with twentyeleven theme.

What am I missing here?!?

EDIT: It appears that if the theme has searchform.php file, this filter will be overridden by that file. So basically filter only works if the theme doesn't have searchform.php. Now that is just stupid..

解决方案

This is a result of a well known WordPress bug in the get_search_form( $echo ) function will ignore the $echo argument and always echo jf you have a searchform.php template file in your theme.

It's a crap function, with a 2 year old trac ticket. Until they fix it, there are few workarounds: 1. Use file_get_contents() - but won't parse the PHP 2. Use custom function with the get_searchform hook (not the actual function) 3. Or use simple output buffering to capture the output as a variable;

This page has examples for all 3 methods

这篇关于WordPress、add_filter、get_search_form的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆