skip to Main Content

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


  1. 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;
    }
    
    Login or Signup to reply.
  2. Many of these actions have filters and output can be removed by returning false.

    // Removes 'meta name="description"' tag from output
    
    add_filter( 'wpseo_metadesc', 'my_custom_metadesc' );
    
    function my_custom_metadesc() {
      return false;
    }
    

    In some cases like WPSEO_Opengraph there’s a filter pattern: wpseo_og_ + property name with underscores instead of colons.

    // Filters '<meta property="article:tag" content="Foo" />'
    
    add_filter( 'wpseo_og_article_tag', 'my_custom_article_tag' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search