• Resolved Blutarsky

    (@blutarsky)


    Hi there guys, I’m here again looking for help. It seems WP documentation sometimes misses in depth help for rookies. ??

    As for my question about filters usage: I see filters can be placed in function.php or in a plugin; it’s unclear if the add_filter statement needs to be coded only once, that is once a filter is applied, it is recorded somewhere in the database and WP manages calling the related function OR if the add_filter statement needs to be there in the “flow” everytime a page is loaded?

    I ask this because I see there’s also an option to remove filters… why is that?

    Can someone clarify?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You use add_filter once to hook your function into the WordPress system. Then your function gets called every time the filter you hooked into gets called. For example:

    function myfilter($var) {
    // code
    }
    add_filter('somefilterhook','myfilter');

    Now, whenever apply_filters('somefilterhook','somevalue'); shows up in the WP source, a theme, or a plugin your function gets called. And, of course, you can call apply_filters yourself if you want. So you can see that it is possible to make big changes using the filters and action hooks. Essentially, they allow you to change some core functions, and some plugin/theme functions, without editing core files.

    remove_filter does the opposite. Why? Well, say you like a plugin except that it applies some weird filter to your post content. You can remove the filter with remove_filter. A fair number of people also seem to not like the built in filter wpautop. You can use remove_filter to get rid of it and then go about your merry way.

    Thread Starter Blutarsky

    (@blutarsky)

    What happens if you place the add_filter call in functions.php?

    It will be executed every time functions.php is loaded?

    Will this scramble WP, because of multiple add_filter calls, or WP will simply ignore subsequent calls?

    It works in functions.php just fine.

    It will run every time functions.php loads, which is once per page load; but since PHP is stateless once per page load is effectively just once.

    Thread Starter Blutarsky

    (@blutarsky)

    This means that the add_filter doesn’t record any value in the WP database permanently, it just “lives” during the “transaction” of all the php documents involved during browser page load. Once the page has been loaded completely in the browser, there are no filters hang somewhere. You load another page, and it all starts from scratch. Correct?

    A function is not run unless it is called…

    When you use an action or filter, you are hooking your filter(or action) onto another existing (wordpress) function, how often it runs is dependant on which function you are hooking onto..

    If i put this in my functions file..

    function hello() {
    echo 'Hello world!';
    }

    ..and nothing more, that function would never run… it’s neither been called or hooked onto a function..

    Adding..

    add_action('get_header','hello');

    .. would instruct the function to run whenever the function get_header is called…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding & removing filters – Rookie question’ is closed to new replies.