0

What would be the best way to filter something that has class members inside? E.g.

class Foo {

   ...constructor, etc. 

   function to_filter() {
      $output = '<div class="wrap">';
      $output .= $this->another_function();
      $output .= more html

      return apply_filters( 'to_filter_name', $output );    
      } 
}

How would I then can use $this->another_function() in my filter function?

0

If this is your own class that you're doing this for, and can modify it, then you could pass the instance of the class as an addition argument to the filter callbacks:

class Foo {

   ...constructor, etc. 

   function to_filter() {
      $output = '<div class="wrap">';
      $output .= $this->another_function();
      $output .= more html

      return apply_filters( 'to_filter_name', $output, $this );    
      } 
}

function filter_foo( $output, $foo ) {
    $output .= $foo->another_function();

    return $output;
}
add_filter( 'to_filter_name', 'filter_foo', 10, 2 );

The important part is that 2 in the add_filter() call. It passes through both arguments to the callback.

  • Unfortunately it's a class from the plugin. – Runnick Jul 23 '17 at 12:37
  • In that case the exact method, and whether it's possible, would depend on the specifc class and function. If you update the question with the actual code you need to use, it will be easier to help. – Jacob Peattie Jul 24 '17 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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