I need to remove the actions that Yoast SEO has added. This is my code:
function remove_actions() {
// deregister all not more required tags
remove_action( 'wp_head', '_wp_render_title_tag', 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'test123' ), 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'front_page_specific_init' ), 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'head' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'head' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'metadesc' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'robots' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'metakeywords' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'canonical' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'adjacent_rel_links' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'publisher' ), 50 );
}
add_action( 'wp_head', 'remove_actions', 1000 );
This code does not remove the actions. What is wrong? How can I remove the actions successfully?
2
Answers
Consider these notes from the remove_action Documentation:
In your case, I believe several of these issues (especially #3 and #4) are causing problems:
First, the priority on your
add_action
is too high. By setting it this high, it’s running after all of the Yoastwp_head
actions are run. Instead, hook into the same action you want to remove, but with a very low number, such as -99999, to cause it to run before the Yoast actions are run. (Further, I’ve broken into two functions, just to be sure they are run at the correct time – one for each action –wp_head
andwpseo_head
).Second, your priorities do not match the priorities in the Yoast code. I’ve dug through all the Yoast code to find all of these actions and documented / corrected in the code below – and I can tell you for example the
metakeywords
hook in Yoast code is 11, so your remove_action (with priority 40) will not work.Finally, Yoast adds these actions to
$this
(an instantiated version of the WPSEO_Frontend class), not static version of the class methods. This means thatremove_action
is not able to find them based on the function array(WPSEO_Frontend
,head
), for example. Instead, you need to load the instantiated version of Yoast, and pass that in to theremove_action
functions.Documented code below:
Final Notes:.
Remove the WPSEO_Frontend::head action is very heavy handed. This will yank a whole host of other things you probably don’t want removed.
Second, It’s probably better to modify the output of these actions, rather than removing them completely.
For example,
Many of these actions have filters and output can be removed by returning
false
.In some cases like WPSEO_Opengraph there’s a filter pattern: wpseo_og_ + property name with underscores instead of colons.