1

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.

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?

3

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.

| improve this answer | |
  • Ok so I followed your directions. And you were right in a sense although the advice had to be applied in reverse. I had to declare the parent class at 'plugins_loaded' and it accounted for all add_filters commands located outside of the parent class. – atwellpub Feb 17 '14 at 22:20

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.