I don’t understand why I need a lower priority to remove an action with a higher priority to make it work

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.

Leave a Comment