Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to modify $path in the following filter. It has 1 input and 2 args.

function documents_template( $template = '' ) {

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

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

This is my function to add filter, it gets error message, how to get it right?

function my_template( $template = '' ){

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

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

I tried to change my return value as following, it doesn't work either:

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

With the belowing answers, my new filter still not working, so, maybe it's because my filter is in a class? here's the completely new code:

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;
 }
}
share|improve this question
add comment

2 Answers

up vote 5 down vote accepted
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 takes 4 variables. The first and second are required. 1. name of the filter, 2. name of the function. The third is the priority (when does the function gets fired). And the fourth is the amount of parameters. If you define the amount of arguments you also have to put them in your function. For example,

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

If the filter supports more arguments (in this case 3)

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

Read all the codex for the information about 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 );

This code works for me. Did you try this?


In your class change:

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

to:

add_filter( 'document_template', array( $this, 'my_template'), 10, 2  );
share|improve this answer
    
add more input in my_template function doesn't work either. –  Jenny Jan 19 '12 at 23:02
    
Do you get an error? –  Rob Vermeer Jan 19 '12 at 23:05
    
yes, all error is the same : call_user_func_array expects parameter 1 to be a valid callback, array must have exactly two members –  Jenny Jan 19 '12 at 23:08
    
I edited my answer. If you remove your code the error is gone, right? –  Rob Vermeer Jan 19 '12 at 23:20
    
yes, I replace my code with this, still not working. –  Jenny Jan 19 '12 at 23:27
show 7 more comments

These need to match, but don't:

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

and

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

document_template != documents_template

Otherwise, everything looks correct.

Edit

Wait, not everything looks correct. I don't think you want to add a parameter to your callback function definition. Instead, you need to define $template within the callback, or simply pass it back unmodified. So, replace this:

function my_template( $template = '' ){

...with this:

function my_template(){

e.g.:

function my_template(){

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

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

Edit 2

Okay, minor mistake on my part. Try this as your callback:

function my_template( $path, $template ){

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

     return $path;
}
add_filter( 'documents_template','my_template', 10, 2 );
share|improve this answer
    
sorry for the mistake of 'document'='documents'. Now with your new function, it's still not working. error says call_user_func_array expects parameter 1 to be a valid callback, array must have exactly two members –  Jenny Jan 19 '12 at 22:44
    
My mistake; you do need to add the parameters to the callback function definition. See updated answer. –  Chip Bennett Jan 19 '12 at 23:34
    
The new code is not working either. I tried print_r, can see my path is correct. –  Jenny Jan 19 '12 at 23:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.