Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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'));
}
}
@IanDelMar
Copy link

IanDelMar commented Dec 11, 2020

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

wpchannel commented Apr 18, 2021

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

@wp-seopress
Copy link

wp-seopress commented Apr 18, 2021

❤️

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