How to add filter with 2 args?

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

Leave a Comment