Skip to content

Instantly share code, notes, and snippets.

@amboutwe
Last active May 25, 2021 22:39
Show Gist options
  • Save amboutwe/1c847f9c706ff6f8c9eca76abea23fb6 to your computer and use it in GitHub Desktop.
Save amboutwe/1c847f9c706ff6f8c9eca76abea23fb6 to your computer and use it in GitHub Desktop.
Remove Yoast from front end for a single post or page
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove Yoast from front end for a single post or page
* Credit: Dodinas https://stackoverflow.com/questions/37845968/disable-wordpress-yoast-seo-on-single-page
* Last Tested: Nov 08 2019 using Yoast SEO 12.4 on WordPress 5.2.4
* Note: For version 14+, please use the code here: https://developer.yoast.com/customization/yoast-seo/disabling-yoast-seo
*********
* DIFFERENT POST TYPES
* Post: Change 123456 to the post ID
* Page: Change is_single to is_page and 123456 to the page ID
* Custom Post Type: Change is_single to is_singular and 123456 to the 'post_type_slug'
Example: is_singular( 'cpt_slug' )
*********
* MULTIPLE ITEMS
* Multiple of the same type can use an array.
Example: is_single( array( 123456, 234567, 345678 ) )
* Multiple of different types can repeat the if statement
*/
add_action( 'template_redirect', 'remove_wpseo' );
function remove_wpseo() {
if ( is_single ( 123456 ) ) {
global $wpseo_front;
if ( defined( $wpseo_front ) ) {
remove_action( 'wp_head', array ($wpseo_front, 'head' ), 1 );
} else {
$wp_thing = WPSEO_Frontend::get_instance();
remove_action( 'wp_head', array( $wp_thing, 'head' ), 1 );
}
}
}
@ipaelo
Copy link

ipaelo commented May 15, 2019

It works like a charm.

Thank you.

@timba64
Copy link

timba64 commented Oct 23, 2019

Thank you for gist! How did you make your profile picture? It`s cool!! 🥇

@amboutwe
Copy link
Author

@timba64 The avatar was created by my colleague. https://yoast.com/evolution-of-yoast-avatars/

@Tommy10Toes
Copy link

I can't seem to get this to work, But i'v got Yoast version 12.4 and WP version 5.2.4 - Any chance this this code needs to be tweaked?

@amboutwe
Copy link
Author

amboutwe commented Nov 8, 2019

@Tommy10Toes I just tested the code and it removed the code output from the front end of the site.

@Tommy10Toes
Copy link

Very odd, This is what I have inside my functions.php file and I just can not get it to work. I have disabled WP Rocket just to be sure, but it does not make a difference.

`<?php
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
function enqueue_child_theme_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}

add_action( 'template_redirect', 'remove_wpseo' );
function remove_wpseo() {
if ( is_page ( 1626 ) ) {
global $wpseo_front;

    if ( defined( $wpseo_front ) ) {
        remove_action( 'wp_head', array ($wpseo_front, 'head' ), 1 );
    } else {
        $wp_thing = WPSEO_Frontend::get_instance();
        remove_action( 'wp_head', array( $wp_thing, 'head' ), 1 );
    }
}

}
`

@amboutwe
Copy link
Author

This is not the proper place to request support. Please check out our extensive Knowledge Base or visit the free support forum. If you require further support, upgrading to our premium version provides you with access to our support team.

@Wouter125
Copy link

Seems like this doesn't work completely. Sometimes the template_redirect hook is not being called. I have one page that I'm serving with dynamic content. You can select filters there, that will narrow down the dynamic content. But as soon as I do this, Yoast is inserting the meta tags on the frontend again. The page get's refreshed and a different url, but the page id remains the same. I'll investigate it a bit more and see if I can figure it out. But if you have any suggestions I would love to hear them.

I still think this is something that should be a setting within the plugin. Especially for a mix of wp pages and dynamic pages. (E.g. content loaded through a JS API).

@fabianrod
Copy link

Is not working for me, maybe there is any update of this code?

@amboutwe
Copy link
Author

@fabianrod This is not the proper place to request support. Please check out our extensive help section or visit the free support forum. If you require further support, upgrading to our premium version provides you with access to our support team.

@APlusDesign
Copy link

This GIST solution will not work with Yoast version 14 and above.

You now need to use.

$front_end = YoastSEO()->classes->get( Yoast\WP\SEO\Integrations\Front_End_Integration::class ); remove_action( 'wpseo_head', [ $front_end, 'present_head' ], -9999 );

@hannahmumma-zz
Copy link

Thanks @APlusDesign. That worked perfectly

@hirasso
Copy link

hirasso commented Feb 1, 2021

If you only deactivate the Yoast's Front_End_Integration, it will still hide the title_tag and some other built-in wordpress filters (front-end-integration.php, line 207 and following:

/**
 * Registers the appropriate hooks to show the SEO metadata on the frontend.
 *
 * Removes some actions to remove metadata that WordPress shows on the frontend,
 * to avoid duplicate and/or mismatched metadata.
 */
public function register_hooks() {
  
  \add_action( 'wp_head', [ $this, 'call_wpseo_head' ], 1 );
  // Filter the title for compatibility with other plugins and themes.
  \add_filter( 'wp_title', [ $this, 'filter_title' ], 15 );

  \add_action( 'wpseo_head', [ $this, 'present_head' ], -9999 );

  \remove_action( 'wp_head', 'rel_canonical' );
  \remove_action( 'wp_head', 'index_rel_link' );
  \remove_action( 'wp_head', 'start_post_rel_link' );
  \remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' );
  \remove_action( 'wp_head', 'noindex', 1 );
  \remove_action( 'wp_head', '_wp_render_title_tag', 1 );
  \remove_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
}

Use this snippet to completely disable YOAST in the frontend:

add_action( 'plugins_loaded', 'disable_yoast_seo_frontend' );
function disable_yoast_seo_frontend() {
  if( is_admin() || !defined('WPSEO_VERSION') ) return;
  $loader = \YoastSEO()->classes->get( \Yoast\WP\SEO\Loader::class );
  \remove_action( 'init', [ $loader, 'load_integrations' ] );
  \remove_action( 'rest_api_init', [ $loader, 'load_routes' ] );
}

Tested with Yoast V. 15.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment