What to try when remove_action doesn’t work

This one might seem like it’s an obvious “DUH” but it’s caught me up more than once, so I figured I’d write myself a note so I don’t spend hours digging thru forums.

Action hooks have to removed after they’ve been added (obviously), but sometimes a hook is added on the callback function of another one like this from the Genesis

[php]
add_action( ‘admin_init’, ‘genesis_add_taxonomy_seo_options’ );
/**
* Add the SEO options to each custom taxonomy edit screen.
*
* @since 1.3.0
*
* @see genesis_taxonomy_seo_options() Callback for SEO fields.
*/
function genesis_add_taxonomy_seo_options() {

foreach ( get_taxonomies( array( ‘show_ui’ => true ) ) as $tax_name )
add_action( $tax_name . ‘_edit_form’, ‘genesis_taxonomy_seo_options’, 10, 2 );

}
[/php]

I kept simply trying to use a remove_action inside of my plugin file, but I could not for the life of me figure out why it would not remove the actions.

It turns out that by the time my code executed, the Genesis hook had not been added, so it could not be removed.

This did work

[php]
add_action("admin_init", "remove_genesis_actions", 11);
function remove_genesis_actions(){
remove_action("dlb-challenge-actions_edit_form","genesis_taxonomy_seo_options",10,2);
remove_action("dlb-challenge-actions_edit_form","genesis_taxonomy_archive_options",10,2);
remove_action("dlb-challenge-actions_edit_form","genesis_taxonomy_layout_options",10,2);
}

[/php]

The moral of the story here, is that when an action that you want to remove is added during the callback function of another hook, you may have to add your remove actions inside of a callback function attached to that original hook with a different priority. (Say that 10 times fast)

In my case, I had to hook my remove_actions onto a later execution of the admin_init hook. This way all of their add_actions were in the queue when I removed mine.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: