如何添加带有2个参数的过滤器?


9

我想在以下过滤器中修改$ path。它具有1个输入和2个args。

function documents_template( $template = '' ) {

       $path = DOCUMENTS_INCLUDES_DIR . '/document/' . $template;

  return apply_filters( 'document_template', $path, $template );
}

这是我添加过滤器的功能,它得到错误消息,如何正确处理?

function my_template( $template = '' ){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
}
add_filter( 'document_template','my_template', 10, 2 );

我试图按以下方式更改返回值,但它也不起作用:

return apply_filters( 'my_template', $path, $template);

有了下面的答案,我的新过滤器仍然无法正常工作,所以,也许是因为我的过滤器在一个类中吗?这是全新的代码:

Class My_Class{
  function __construct() {
     add_filter( 'document_template', array( $this, 'my_template',10, 2 ) );
  }
 function my_template( $path, $template ){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
 }
}

Answers:


10
function my_locate_template( $path, $template ){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
}
add_filter( 'documents_template','my_locate_template', 10, 2 );

add_filter具有4个变量。第一个和第二个是必需的。1.过滤器的名称,2.函数的名称。第三个是优先级(何时触发该功能)。第四个是参数数量。如果定义参数的数量,则还必须将其放入函数中。例如,

add_filter( 'the_filter','your_function', 10, 1 );
function your_function($var1) {
   // Do something
}

如果过滤器支持更多参数(在这种情况下为3)

   add_filter( 'the_filter','your_function', 10, 3 );
    function your_function($var1, $var2, $var3) {
       // Do somthing
    }

阅读所有法典以获取有关add_filter()的信息


function documents_template( $template = '' ) {

       $path = DOCUMENTS_INCLUDES_DIR . '/document/' . $template;

  return apply_filters( 'document_template', $path, $template );
}

function my_template( $path, $template ){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
}
add_filter( 'document_template','my_template', 10, 2 );

该代码对我有用。你有尝试过吗?


在您的班级变更中:

add_filter( 'document_template', array( $this, 'my_template',10, 2 ) );

至:

add_filter( 'document_template', array( $this, 'my_template'), 10, 2  );

在my_template函数中添加更多输入也不起作用。
珍妮

你有错误吗?
Rob Vermeer

是的,所有错误均相同:call_user_func_array期望参数1为有效的回调,数组必须恰好具有两个成员
Jenny

我编辑了答案。如果删除代码,错误消失了,对吗?
罗伯·维尔梅尔

是的,我用此代码替换了代码,仍然无法正常工作。
珍妮

2

这些需要匹配,但不需要:

apply_filters( 'document_template', $path, $template );

add_filter( 'documents_template','my_template', 10, 2 );

document_template != documents_template

否则,一切看起来都正确。

编辑

等等,并非一切看起来都正确。我认为您不想在回调函数定义中添加参数。取而代之的是,您需要$template在回调中定义,或直接将其传递回未经修改的状态。因此,替换为:

function my_template( $template = '' ){

...有了这个:

function my_template(){

例如:

function my_template(){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
}
add_filter( 'documents_template','my_template', 10, 2 );

编辑2

好吧,我的小失误。尝试将此作为您的回调:

function my_template( $path, $template ){

      $path = MY_INCLUDES_DIR . '/document/'. $template;

     return $path;
}
add_filter( 'documents_template','my_template', 10, 2 );

抱歉'document'='documents'的错误。现在有了新功能,它仍然无法正常工作。错误说call_user_func_array期望参数1为有效的回调,数组必须恰好具有两个成员
Jenny

我的错; 您确实需要将参数添加到回调函数定义中。查看最新答案。
Chip Bennett 2012年

新代码也不起作用。我试过print_r,可以看到我的路径是正确的。
珍妮
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.