• Resolved Rajarshi Bose

    (@truthsearcher83)


    I am unable to understand how add_filter() is processed by WordPress . I was playing around with custom hooks and found that add_filter(‘filter_tag’ , $var ) has to be before apply_filter(‘filter_tag , ‘callback’) while using a custom filter . So a code like below doesnt work :

    function callback($var){
        return ($var.'append');
    }
    
    $var = 'testing';
    echo $var;
    $var1 = apply_filters('custom_filter', $var);
    add_filter('custom_filter' , 'callback');
    echo $var1;

    Instead it needs to be :

    add_filter('custom_filter' , 'callback');
    $var1 = apply_filters('custom_filter', $var);

    What I am facing a hard time understanding is when we hook into another plugin which has filter hooks how is my add_filter('custom_filter' , 'callback'); executed before the plugin’s apply_filters('custom_filter', $var); ? WordPress most be executing code from different plugins sequentially .When different plugins are being executed aren’t their any chances of WordPress encountering a apply_filters('custom_filter', $var); without the corresponding apply_filters('custom_filter', $var); ?

    Hope my question makes sense .

    • This topic was modified 5 years, 3 months ago by Jan Dembowski. Reason: Formatting
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    apply_filters() grabs all callbacks in the global $wp_filter array that are registered for its specific tag and calls them each in turn.

    add_filter() simply adds the passed callback to the global $wp_filter array. So if you add after applying, the callback was not in place during the apply call. It is always the case that you must add before applying. If you added a callback to the “init” action in a header template, it will never execute because “init” fires well before header template code executes.

    Plugins load very early, so their setup is in place before most things happen in WP. In any case, plugins generally apply_filters() within a function call which executes during output, so adding to that filter from functions.php or another plugin will always be before the apply_filters() call. It’s not the order in which PHP is parsed that matters, it is when the code executes. Plugin code is parsed very early, but the functions declared often do not execute until output occurs.

    Of course waiting until output is not always the case, but at least waiting until “init” action is nearly always the case. Anything earlier is unstable. Since we typically add before “init”, our callbacks are invariably in place before application.

    I hope this helps with your understanding. it is a queer thing to grasp at first. Once you understand it though, you’ll be like “Duh! Of course! It’s so simple.” Before then, it is tricky to grasp the concept.

    Thread Starter Rajarshi Bose

    (@truthsearcher83)

    After some experimentation this absolutely makes sense . One of the best answers I’d ever got . Thanks a lot .

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. Yes, experimenting for yourself until something is clear is the best way to understand something.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Unable to understand add_filter() for custom hook’ is closed to new replies.