Skip to content

Instantly share code, notes, and snippets.

@wpchannel
Last active April 16, 2023 08:33
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpchannel/7cdd6eed0927ea5732d7 to your computer and use it in GitHub Desktop.
Save wpchannel/7cdd6eed0927ea5732d7 to your computer and use it in GitHub Desktop.
Hide annoying notifications after each upgrade of Yoast SEO plugin and others admin notices
<?php if (!defined('ABSPATH')) die('Restricted Area');
/*
* Plugin Name: Disable Yoast SEO Notifications
* Description: Hide annoying notifications after each upgrade of Yoast SEO plugin and others admin notices.
* Version: 1.1
* Author: Aurélien Denis
* Author URI: https://wpchannel.com/
*/
add_action('admin_init', 'wpc_disable_yoast_notifications');
function wpc_disable_yoast_notifications() {
if (is_plugin_active('wordpress-seo/wp-seo.php')) {
remove_action('admin_notices', array(Yoast_Notification_Center::get(), 'display_notifications'));
remove_action('all_admin_notices', array(Yoast_Notification_Center::get(), 'display_notifications'));
}
}
@leesaenz
Copy link

leesaenz commented Nov 18, 2016

@wpchannel I think @austinginder's code is better than your suggestion because your suggestion removes the entire Yoast menu form the admin bar, whereas @austinginder simply removes the alert bubbles and keeps the menu.

Also, the main code above in this gist doesn't work anymore.

@Bobz-zg
Copy link

Bobz-zg commented Feb 15, 2017

Thanks. I used to love this plugin before they start BS with UI.

Copy link

ghost commented Mar 5, 2019

Anyone has an idea how to remove the notices generated by wordpress-seo/admin/watchers/class-slug-change-watcher.php? I opened a ticket to have the notices removed but yoast with their uppity refused. Yoast/wordpress-seo#11520

@Interinactive
Copy link

@matto15 this is what I use to hide all of the Yoast crap continually popping up:

	add_action('admin_head', 'adminStyles');
	function adminStyles() {
	  echo '<style>
		.yoast-alert {
			display: none !important;
		}
	  </style>';
	}

Copy link

ghost commented Apr 21, 2019

@Interinactive. Thanks. I will give it a try.

@emcniece
Copy link

September 2019 checking in, @Interinactive's comment works wonders 👍

@trivendor
Copy link

There are two areas that could really use being addressed - when using Yoast Premium without activation (which may or may not be within intent of this thread)

  1. on plugins page -- yoast makes HTTP calls to headquarters
  2. in yoast seo meta box in post editor -- it loads a warning box using javascript that craftfully loads after everything else so that CSS display none won't work. the js itself is so convoluted that only an expert could unravel it.

would appreciate ideas/fixes for these, thanks

@Interinactive
Copy link

@trivendor How do you make it appear for #2? It's probably not very hard to get around it, but I need to see what you're referring to.

@trivendor
Copy link

@Interinactive -- thanks for responding, if you go to any post/page/cpt post editor page, there is the Yoast SEO meta box area that features things like snippet editor etc.

Yoast js loads a yellow nag about subscribing
this comes out of wp-seo-premium-metabox-1160.min.js and is likely tied to other js somehow
using CSS display none for the div doesn't work - either because of something in the javascript or that it loads CSS after the admin css or ?
yoast-yellow

@wpchannel
Copy link
Author

Hi there, I'm the author of the Gist. Since I'm no more using Yoast (I prefer SEOPress with no ads), I can't help you anymore...

@Interinactive
Copy link

Interinactive commented Oct 10, 2019

@trivendor Still works here. You need to find the name of the div, eg:

https://i.imgur.com/z7CS3yg.png

Then amend the code to include that class:

	add_action('admin_head', 'adminStyles');
	function adminStyles() {
	  echo '<style>
		.yoast-alert,
	        .MetaboxWarning__WarningBox-gHrSIw {
			display: none !important;
		}
	  </style>';
	}

Works perfectly

@trivendor
Copy link

Nice ! People do need to be aware that each instance adds a variable string... -gHrSIw is not universal and another string will be used.

But this works great, thanks

@Interinactive
Copy link

Yep that's right. And even if they randomised them with each page load, you could still use certain selectors to catch it. Eg: 'container-name + div', and so on.

@emcniece
Copy link

If this plugin has any semblance of packaging or CI build processing (which is almost certainly has) then this CSS class will not only change between versions, it could also differ between installations. I've inspected the last 4 versions of this plugin and can't find gHrSlw anywhere, likely because I'm only looking at the free versions.

This gist should be converted to a plugin, and the nags should be removed by finding and overriding the injecting hooks instead of somewhat-fragile CSS if possible. I'll try and take a stab at this over the weekend... I'm sure @wpchannel wouldn't mind receiving fewer notifications 😂

@trivendor if you have a second, could I ask you to reach out to me at emcniece@gmail.com? I've got a question about the plugin version you currently have.

For now, you might consider using CSS wildcard selectors for slightly longer-term relief (untested concept):

add_action('admin_head', 'adminStyles');
function adminStyles() {
  echo '<style>
    .yoast-alert,
    div[class*='MetaboxWarning__WarningBox-'] {
        display: none !important;
    }
  </style>';
}

@trivendor
Copy link

Hi @emcniece yes the string gHrSlw will change to something else - you'd have to look at the generated css when installed and change snippet accordingly. I will try the div without the exact string next go around but it is working with the edit of my particular instance.

regards

@wpchannel
Copy link
Author

wpchannel commented Oct 14, 2019

Hi there,

I've found an old mu-plugin in my computer if you wan to try a more complet solution:

`<?php include_once(ABSPATH . 'wp-admin/includes/plugin.php');
if (is_plugin_active('wordpress-seo/wp-seo.php')) {

/* Remove HTML Comments */
add_action('get_header',function() { ob_start(function ($o) {
return preg_replace('/\n?<.*?Yoast SEO plugin.*?>/mi','',$o); }); });
add_action('wp_head',function() { ob_end_flush(); }, 999);


/* Disable Yoast SEO Notifications */
function ntp_disable_yoast_notifications() {
	remove_action('admin_notices', array(Yoast_Notification_Center::get(), 'display_notifications'));
	remove_action('all_admin_notices', array(Yoast_Notification_Center::get(), 'display_notifications'));
}
add_action('admin_init', 'ntp_disable_yoast_notifications');


/* Yoast SEO Low Priority */
function ntp_yoast_bottom() {
	return 'low';
}
add_filter('wpseo_metabox_prio', 'ntp_yoast_bottom');


/* Disable screen after update */
function ntp_filter_yst_wpseo_option($option) {
	if (is_array($option)) { 
		$option['seen_about'] = true; 
	}
	return $option;
}
add_filter('option_wpseo', 'ntp_filter_yst_wpseo_option');


/* Remove Node in Toolbar */
function ntp_remove_yoast_bar($wp_admin_bar) {
	$wp_admin_bar->remove_node('wpseo-menu');
}
add_action('admin_bar_menu', 'ntp_remove_yoast_bar', 99);

} ?>`

@Braunson
Copy link

Braunson commented Mar 18, 2020

@wpchannel Thank you, for formatting didn't seem to come through.

For anyone who want's to use this, drop this code in your theme's function.php file

/** Hide Yoast SEO alert from everywhere **/
if (is_plugin_active('wordpress-seo/wp-seo.php')) {
    
    /* Remove HTML Comments */
    add_action('get_header',function() { ob_start(function ($o) {
    return preg_replace('/\n?<.*?Yoast SEO plugin.*?>/mi','',$o); }); });
    add_action('wp_head',function() { ob_end_flush(); }, 999);
    
    
    /* Disable Yoast SEO Notifications */
    function ntp_disable_yoast_notifications() {
    	$ync = Yoast_Notification_Center::get();
    	remove_action('admin_notices', array($ync, 'display_notifications'));
    	remove_action('all_admin_notices', array($ync, 'display_notifications'));
    }
    add_action('admin_init', 'ntp_disable_yoast_notifications');
    
    
    /* Yoast SEO Low Priority */
    function ntp_yoast_bottom() {
    	return 'low';
    }
    add_filter('wpseo_metabox_prio', 'ntp_yoast_bottom');
    
    
    /* Disable screen after update */
    function ntp_filter_yst_wpseo_option($option) {
    	if (is_array($option)) { 
    		$option['seen_about'] = true; 
    	}
    	return $option;
    }
    add_filter('option_wpseo', 'ntp_filter_yst_wpseo_option');
    
    
    /* Remove Node in Toolbar */
    function ntp_remove_yoast_bar($wp_admin_bar) {
    	$wp_admin_bar->remove_node('wpseo-menu');
    }
    add_action('admin_bar_menu', 'ntp_remove_yoast_bar', 99);
}

@wpchannel
Copy link
Author

Thanks!

@IanDelMar
Copy link

I'd suggest to don't get the Yoast_Notification_Center instance twice and to check whether the class exists.

// Hide Yoast SEO alert from everywhere.
if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) ) {

	// Remove HTML Comments.
	add_action( 'get_header', function() { 
		ob_start( function( $o ) {
			return preg_replace( '/\n?<.*?Yoast SEO plugin.*?>/mi', '', $o ); 
		} );
	} );
	add_action( 'wp_head', function() { ob_end_flush(); }, 999 );


	// Disable Yoast SEO Notifications.
	function ntp_disable_yoast_notifications() {
		if ( class_exists( 'Yoast_Notification_Center' ) ) {
			$ync = Yoast_Notification_Center::get();
			remove_action( 'admin_notices', array( $ync, 'display_notifications' ) ) ;
			remove_action( 'all_admin_notices', array( $ync, 'display_notifications' ) );
		}
	}
	add_action( 'admin_init', 'ntp_disable_yoast_notifications' );


	// Yoast SEO Low Priority.
	function ntp_yoast_bottom() {
		return 'low';
	}
	add_filter('wpseo_metabox_prio', 'ntp_yoast_bottom');

	// Disable screen after update.
	function ntp_filter_yst_wpseo_option( $option ) {
		if ( is_array( $option ) ) { 
			$option['seen_about'] = true; 
		}
		return $option;
	}
	add_filter( 'option_wpseo', 'ntp_filter_yst_wpseo_option' );


	// Remove Node in Toolbar.
	function ntp_remove_yoast_bar( $wp_admin_bar ) {
		$wp_admin_bar->remove_node( 'wpseo-menu' );
	}
	add_action( 'admin_bar_menu', 'ntp_remove_yoast_bar', 99 );
}

@primathon
Copy link

primathon commented Apr 16, 2021

Since Yoast refuses to cut back on the the upsell spam, here's a snippet to remove all the "You trashed a thing! Buy our premium plugin!" notices that they clutter up the admin dashbaord with. Also recommend looking into the Hide SEO Bloat plugin which basically exists solely to remove all the crap that Yoast adds all over your site. Anyway, put this code in your functions.php (or use something like Code Snippets):

/**
 * Unbind Yoast's awful constant upsell notifications whenever you trash/delete anything
 *
 * @ref: https://github.com/Yoast/wordpress-seo/blob/0742e9b6ba4c0d6ae9d65223267a106b92a6a4a1/admin/watchers/class-slug-change-watcher.php#L18
 * @see: https://wordpress.stackexchange.com/a/352509
 */
function unbind_yoast_slug_change_watchers()
{
  $priority = 10;
  $actions_methods = [
    'wp_trash_post'        => 'detect_post_trash',
    'before_delete_post'   => 'detect_post_delete',
    'delete_term_taxonomy' => 'detect_term_delete',
  ];
  
  global $wp_filter;
  foreach ($actions_methods as $action => $method)
  {
    if (isset($wp_filter[$action]->callbacks[$priority]) and ( ! empty($wp_filter[$action]->callbacks[$priority])))
    {
      $wp_filter[$action]->callbacks[$priority] = array_filter($wp_filter[$action]->callbacks[$priority], function($v, $k) use ($method) {
        return (stripos($k, $method) === false);
      }, ARRAY_FILTER_USE_BOTH );
    }
  }
}
add_action('plugins_loaded', 'unbind_yoast_slug_change_watchers', 20);

Hopefully this makes things a little nicer for anyone using it.

@wpchannel
Copy link
Author

@primathon : thanks for sharing! For me the best solution was to switch to SEOPress. :)

@wp-seopress
Copy link

❤️

@Beee4life
Copy link

Can it be the notice is now shown 'through' JS ?

I just noticed a pesky popup showing again after deleting a term (with the function @primathon shared in my functions).

@wpchannel
Copy link
Author

@Beee4life I don't know because I'm using SEOPress PRO for the vast majority of my websites now. No ads.

@Beee4life
Copy link

@wpchannel then why reply if you don't know or even use the plugin (anymore)

@emcniece
Copy link

emcniece commented Apr 16, 2023

Because they're the author of this gist, they get notifications for comments here, and they take responsibility for their work and want to help people that land here? Strange question.

If you mean to say "why reply if you can't answer my question" perhaps consider they did provide a solution, which is to use a different plugin.

@Beee4life
Copy link

We clearly have different opinions on what helpful replies are... because the question at hand is about a notice shown through js. Not whether I should use a different plugin or not... anyway, anyone who reads this, enjoy your day...

@wpchannel
Copy link
Author

@Beee4life I'm the author of this gist, and I'm always trying to answer to comments even after years like I do on hundreds of tutorials published on my blog. The original problem to solve was to hide annoying notifications from Yoast plugin: this gist did the job for years. But now, the answer that I give to people - not only you in my previous answer, is simple: you don't like ads, change your SEO plugin to another. I'm tired of updating my mu-plugins every 6 months just to hide an advert. That's my solution, maybe you prefer another one and if you take time to find the correct hook, I'll be happy to update my gist with your code. ;)

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