0

I have a really newbie question, but I'm really new to WordPress development. In the plugin that I'm using is using this filter.

if ( true === apply_filters( 'some_custom_filter', false ) ) {
   return $something;
}

Now I'd like to add_filter so it will return true how do I achieve that?

This is what I'm trying but doesn't work it always return false

public function setup_filters() {
    add_filter( 'some_custom_filter', array( $this, 'filter_suppress_the_content' ), 10, 3 );
}

public function filter_suppress_the_content() {
    return true;
}

This is the class.

private static $instance;
private static $wpcom_related_posts;

public static function get_instance() {
    if( ! isset( self::$instance ) ) {
        self::$instance = new Klazz;
        self::$instance->setup_filters();
    }

    return self::$instance;
}

public function setup_filters() {
    add_filter( 'some_custom_filter', array( $this, 'filter_suppress_the_content' ), 10, 1 );
}

public function filter_suppress_the_content( $false) {
    return true;
}
1

You are almost close.

add_filter( 'some_custom_filter', array( $this, 'filter_suppress_the_content' ), 10, 3 );
// the above line states that, the method `filter_suppress_the_content` should have three arguments, where you have used nothing.


public function filter_suppress_the_content() {
    return true;
}
// comparing your code, this method should have one argument

The solution:

public function setup_filters() {
  add_filter( 'some_custom_filter', array( $this, 'filter_suppress_the_content' ), 10, 1 );
}
public function filter_suppress_the_content( $false ){
    return true;
}
5
  • I tried with your code it's still returning false
    – toy
    Apr 21 '15 at 0:18
  • Exactly where did you called the setup_filters method ? please include the full code. Note: apply_filters should be called before add_filter.
    – Shazzad
    Apr 21 '15 at 0:20
  • I've included the fullcode and apply_filters is being called in the plugin.
    – toy
    Apr 21 '15 at 0:22
  • That's it I have to call it before apply_filters thanks!
    – toy
    Apr 21 '15 at 0:25
  • 1
    Awesome :) Note: WP has some quick functions to make things easier. Such as __return_false, __return_true, __return_empty_array etc. So for doing true/false type return, you can just use them quickly. ex: add_filter( 'some_custom_filter', '__return_false');
    – Shazzad
    Apr 21 '15 at 0:28

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.