How can I change action priority in Wordpress?

18,227

Solution 1

From WordPress

if a hook was registered using a priority other than the default of 10, then you must also specify the priority in the call to remove_action().

So I think you can first remove using following

remove_action('thematic_header', 'thematic_brandingopen', 1);
remove_action('thematic_header', 'thematic_access', 9);

and the add again using different priority

add_action('thematic_header', 'thematic_access', 1);
add_action('thematic_header', 'thematic_brandingopen', 2);

Solution 2

not to self-promote but I have done some work on this to provide a non-coding solution through a WordPress plugin called Prioritize Hooks. My plugin lets you set the priorities of various registered hooks through a UI and does the overriding in runtime so the code isn't modified.

Solution 3

Just in case this helps someone, the variable actions are stored in is

global $wp_filter;
var_dump( $wp_filter[$hook_name] );

Which is an array of arrays with the keys being the priority when the action was added.

Share:
18,227

Related videos on Youtube

eldarshamukhamedov
Author by

eldarshamukhamedov

Updated on August 03, 2022

Comments

  • eldarshamukhamedov
    eldarshamukhamedov over 1 year

    I'm using the Thematic framework for a child theme. It has a number of hooks, but I'm looking at thematic_header() in particular. The thematic_header() hook adds the following actions (through add_action):

    <?php
      add_action('thematic_header', 'thematic_brandingopen', 1);
      add_action('thematic_header', 'thematic_blogtitle', 3);
      add_action('thematic_header', 'thematic_blogdescription', 5);
      add_action('thematic_header', 'thematic_brandingclose', 7);
      add_action('thematic_header', 'thematic_access', 9);
    ?>
    

    The content of the actions is irrelevant.

    My question is this: How can I change the priorities of the five actions in question. For instance, I want thematic_access() to load before thematic_brandingopen(). Only way to do this that I've been able to figure out is by removing and re-adding the actions, ala:

    <?php
      function remove_thematic_actions() {
        remove_action('thematic_header', 'thematic_access');
        add_action('thematic_header', 'thematic_access', 0); //puts it above thematic_brandingopen
      } 
      add_action ('init', 'remove_thematic_actions');
    

    That seems like a stupid way of accomplishing something very simple. Is there a way to access and sort/reorder whatever data structure stores actions in WP?

  • eldarshamukhamedov
    eldarshamukhamedov over 11 years
    Yeah, that's pretty much what I've been doing. My question was whether there was a way to directly access the array/whatever WP uses to store action priorities. I'll keep doing it this way since I'm too lazy to dig further into WP source. It just seems strange to me that WP doesn't provide a way to change action priorities without removing and re-adding them. Cheers
  • The Alpha
    The Alpha over 11 years
    I think it's a class property and could be declared as private/protected but not public. May be that is not possible to access directly, IMO.
  • eldarshamukhamedov
    eldarshamukhamedov over 11 years
    Thanks! I was looking for a coding solution, but, at the very least, this is an interesting solution. Keep up the good work! :)
  • lukenofurther
    lukenofurther over 8 years
    Just in case this helps someone, all hooked actions are stored in a global variable called $wp_filter, which is an array with the key being the hook name.
  • Exit
    Exit over 8 years
    This helped in that I had a hard time removing an action that was within a class using a class method as the callback. There are various answers as to how to remove the action "properly", but they are quite involved and work conditionally. I just added global $wp_filter; and unset($wp_filter['hookname'][priority]);