WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need get_filters() to push-and-pop current filter settings (4 posts)

  1. wlindley
    Member
    Posted 2 years ago #

    I have a plugin which uses filters to handle what-to-output.

    Now I am writing a widget that will call the plugin directly, but would like to override the filters on a per-widget-instance basis. That means I need to "push" or save my plugin's filters, set them, call the plugin, and "pop"/restore the filter settings.

    Unfortunately altho wordpress has a has_filter() to see whether a specific function is hooked to a filter, the user's theme or other plugins could have set the filters to be anything.

    I studied plugin.php and discovered WP has no get_filters() to retrieve the entire current list of functions assigned to a filter. There really should be a get_filters($tag) and a get_actions($tag) which basically return what is currently WP's internal array.

    I would prefer not to hack WP's internal variables but see no alternative at this point. Any ideas?

  2. bcworkz
    Member
    Posted 2 years ago #

    Since you know the function names of your own filter, you can use remove_filter() and remove_action() to take your own plugin out of action, then use add_filter() and add_action() to restore afterwards.

    You're not really supposed to need to be concerned about other hooks into a particular filter or action, thus the lack of a listing function. Of course, things don't always work out so well, and it would be good to know.

  3. wlindley
    Member
    Posted 2 years ago #

    Except that, other people's themes are supposed to override the defaults that I created, and put their own filter-sets in. If a widget comes along and clobbers that, all the other invocations of the plugin will be broken... that is why I need to save the filter state and politely restore it.

  4. wlindley
    Member
    Posted 2 years ago #

    To answer my own question, here are two rather unfortunately hacked subroutines that will return the entire current state of a filter, and reset a filter to such a saved state.

    function get_filter($tag) {
      # Returns the current state of the given WordPress filter.
      global $wp_filter;
      return $wp_filter[$tag];
    }

    and then

    function set_filter($tag, $saved) {
      # Sets the given WordPress filter to a state saved by get_filter.
      remove_all_filters($tag);
      foreach ($saved as $priority => $func_list) {
        foreach ($func_list as $func_name => $func_args) {
          add_filter($tag,$func_args['function'], $priority, $func_args['accepted_args']);
        }
      }
    }

    Example usage:

    $was_filter = get_filter('autonav_create_list_item');
    # Erase filters
    remove_all_filters('autonav_create_list_item');
    # .... do something clever with the filter
    # and now restore it to the user's settings
    set_filter('autonav_create_list_item', $was_filter );

Topic Closed

This topic has been closed to new replies.

About this Topic