add_filter to apply_filters that exists within class

I’m still new to Classes.

I have a WordPress issue where I can only use add_filters within a class on a apply_filters command that exists within the same class.

Read More

Here’s an example class that successfully adds a filter:

$CTAExtensions = new CTALoadExtensions;

class CTALoadExtensions
{
    public $definitions;
    public $template_categories;

    function __construct()
    {       
        $this->add_core_definitions();
        $this->load_definitions();  
    }


    /* collects & loads extension array data */
    function load_definitions()
    {
        $wp_cta_data = $this->template_definitions;
        $this->definitions = apply_filters( 'wp_cta_extension_data' , $wp_cta_data);
    }

    /* filters to add in core definitions to the calls to action extension definitions array */
    function add_core_definitions()
    {
        add_filter('wp_cta_extension_data' , array( $this , 'add_advanced_settings') , 1  );

    }

    function add_advanced_settings()
    {
        echo 1;exit; works!
    }
}

If I try to add the filter outside of the class like this… I have no luck:

add_filter( "wp_cta_extension_data", "wp_cta_bt_meta_boxes" , 10 , 1 );
function wp_cta_bt_meta_boxes( $wp_cta_data )
{
    echo 1; exit; //does not work outside of class.

    return $wp_cta_data;
}

I’ve even tried extending the class and adding the filter to see if it will work but without success:

$wp_cta_bt = new CTABehavioralExtend;

class CTABehavioralExtend extends CTALoadExtensions 
{
    function __construct(){
        add_filter( "wp_cta_extension_data", array( $this , "add_advanced_settings" ) , 10 , 1 );
    }

    function add_advanced_settings( $wp_cta_data )
    {
        echo here;exit; // does not work.




        return $wp_cta_data;
    }
}

Does anyone have any insight to what I am not understanding?

Related posts

1 comment

  1. I suspect the issue has to do with the load order of the plugins / filters.

    The plugin that has the the apply_filters() call (inside of CTALoadExtensions) is running after the plugin that has the add_filter() call.

    Try putting add_filter() inside of a function that is tied to plugins_loaded.

Comments are closed.