Home How to remove actions from Yoast SEO plugin
Reply: 1

How to remove actions from Yoast SEO plugin

bradley546994
1#
bradley546994 Published in 2018-01-13 00:39:29Z

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?

cale_b
2#
cale_b Reply to 2018-01-13 15:37:28Z

Consider these notes from the remove_action Documentation:

  1. You may need to prioritize the removal of the action to a hook that occurs after the action is added.
  2. You cannot successfully remove the action before it has been added.
  3. You also cannot remove an action after it has been run.
  4. To remove an action the priority must match the priority with with the function was originally added.

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 Yoast wp_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 and wpseo_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 that remove_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 the remove_action functions.

Documented code below:

// Remove ONLY the head actions.  Permits calling this at a "safe" time
function remove_head_actions() {
    // not Yoast, but WP default. Priority is 1
    remove_action( 'wp_head', '_wp_render_title_tag', 1 );

    // If the plugin isn't installed, don't run this!
    if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
        return;
    }

    // Get the WPSEO_Frontend instantiated class
    $yoast = WPSEO_Frontend::get_instance();
    // removed your "test" action - no need
    // per Yoast code, this is priority 0
    remove_action( 'wp_head', array( $yoast, 'front_page_specific_init' ), 0 );
    // per Yoast code, this is priority 1
    remove_action( 'wp_head', array( $yoast, 'head' ), 1 );
}

function remove_wpseo_head_actions() {
    // If the Yoast plugin isn't installed, don't run this
    if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
        return;
    }

    // Get the Yoast instantiated class
    $yoast = WPSEO_Frontend::get_instance();
    remove_action( 'wpseo_head', array( $yoast, 'head' ), 50 );
    // per Yoast code, this is priority 6
    remove_action( 'wpseo_head', array( $yoast, 'metadesc' ), 6 );
    // per Yoast code, this is priority 10
    remove_action( 'wpseo_head', array( $yoast, 'robots' ), 10 );
    // per Yoast code, this is priority 11
    remove_action( 'wpseo_head', array( $yoast, 'metakeywords' ), 11 );
    // per Yoast code, this is priority 20
    remove_action( 'wpseo_head', array( $yoast, 'canonical' ), 20 );
    // per Yoast code, this is priority 21
    remove_action( 'wpseo_head', array( $yoast, 'adjacent_rel_links' ), 21 );
    // per Yoast code, this is priority 22
    remove_action( 'wpseo_head', array( $yoast, 'publisher' ), 22 );
}

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,

add_action('wpseo_metakeywords', 'your_metakeywords_function');

function your_metakeywords_function( $keywords ) {
    // modify the keywords as desired
    return $keywords;
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.429165 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO