0

In the section about Priority of the WordPress documentation is stated:

A function with a priority of 11 will run after a function with a priority of 10; and a function with a priority of 9 will run before a function with a priority of 10. Any positive integer is an acceptable value, and the default value is 10.

All clear, nothing to say about this.

But, I had a problem removing an action already added (the question is this).

To add the action the code used is this:

add_action( 'genesis_entry_header', 'shq_genestrap_post_meta_categories', 9 );

So, the expected code to remove it should be this:

// Expected that a priority of 10 remove the added action
// This DOESN'T WORK
add_action('genesis_entry_header', 'my_remove', 10);

function my_remove() {
    remove_action( 'genesis_entry_header', 'shq_genestrap_post_meta_categories', 9 );
}

But with this priority the action is not removed.

Instead, to make it work, I have to use a priority of 8:

// A priority of 8, instead, unexpectedly works:
add_action('genesis_entry_header', 'my_remove', 10);
...

What I don't understand is why it works: if I use a priority of 8 the action I'm going to remove is not yet added as it has a priority of 9: instead, a priority of 8 works.

Why?

5

You're misunderstanding how priority works here somewhat. The priority determines the order that the functions run for a given hook, eg. genesis_entry_header. Priority is not global.

When removing an action from a hook you need to use the same priority that the action was originally hooked with. Which you're doing.

You're adding the action at priority of 9:

add_action( 'genesis_entry_header', 'shq_genestrap_post_meta_categories', 9 );

And using the matching priority of 9 to remove it:

remove_action( 'genesis_entry_header', 'shq_genestrap_post_meta_categories', 9 );

Your actual problem is that you can't remove an action before it's been added, or after it's already been run.

Let's look at the two action hooks you're running outside of functions:

add_action('genesis_entry_header', 'my_remove', 10);
add_action( 'genesis_entry_header', 'shq_genestrap_post_meta_categories', 9 );

What's going to happen is that Genesis is going to trigger the genesis_entry_header hook somewhere in its code or templates with this:

do_action( 'genesis_entry_header' );

When that happens your two functions are going to run in the order of their priority:

shq_genestrap_post_meta_categories(); // Priority 9
my_remove(); // Priority 10

You're attempting to unhook shq_genestrap_post_meta_categories() inside my_remove(), but that's not going to work, because it's already happened.

When you change the priority to 8, this happens:

my_remove() // Priority 8
// Priority 9, nothing is hooked anymore.

my_remove() is running before shq_genestrap_post_meta_categories(), so my_remove() will be able to successfully unhook it.

So the key takeaway is: To remove an action, the remove_action() function needs to be run:

  1. After the action you want to remove has been added with add_action().
  2. Before the action is run.

The reason whoever came up with your solution has told you to hook your function like this...

add_action('genesis_entry_header', 'my_remove', 8);
function my_remove() {
    remove_action( 'genesis_entry_header', 'shq_genestrap_post_meta_categories', 9 );
}

... is because genesis_entry_header at priority 8 is the last possible moment before the function you want to unhook will run. This means you can be almost certain that the two conditions I mentioned have been met: The action has been added, but has not run yet.

It's quite likely that you can actually remove the action far earlier, but it's not possible to say exactly when without knowing exactly what order all your code is loaded in, which a 3rd-party observer is unlikely to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.