Forums

Understanding Filters? (4 posts)

  1. tech-otaku
    Member
    Posted 1 year ago #

    I'm trying to understand what the call to apply filters is doing in this piece of code from the WP_Widget_Categories class in wp-includes/default-widgets.php.

    $cat_args['title_li'] = '';
    wp_list_categories(apply_filters('widget_categories_args', $cat_args));

    My understanding is that the value of $cat_args is being passed to all the functions associated with the widget_categories_args tag and that this association is made using add_filter('widget_categories_args', filter_function).

    However, I don't think my understanding can be correct as I can't find any reference to add_filter('widget_categories_args', filter_function) anywhere within the WP code.

    It seems I've searched everywhere but I'm just not getting it. Could someone shed a little light on how filters work in general.

    Many, many thanks in advance. Steve.

  2. Mark / t31os
    Moderator
    Posted 1 year ago #

    apply_filters sets up a filter hook, it doesn't necessarily mean any filters exist for that hook, which is why you aren't able to find any references to add_filter('widget_categories_args',.

    An example filter.

    add_filter('widget_categories_args','list_cats_args_filter');
    
    function list_cats_args_filter( $cat_args ) {
        $cat_args['title_li'] = 'My filter provided title';
        return $cat_args;
    }
  3. tech-otaku
    Member
    Posted 1 year ago #

    Thanks for clarifying that Mark. I think I've got it.

    Regards, Steve.

  4. Mark / t31os
    Moderator
    Posted 1 year ago #

    You're welcome.. :)

    Further examples can be found here.
    http://codex.wordpress.org/Plugin_API/Hooks_2.0.x

Topic Closed

This topic has been closed to new replies.

About this Topic