Skip to content

Instantly share code, notes, and snippets.

@omniacode
Created October 24, 2017 20:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save omniacode/94f3452ad1e278b895e0ff8b42526fa2 to your computer and use it in GitHub Desktop.
Save omniacode/94f3452ad1e278b895e0ff8b42526fa2 to your computer and use it in GitHub Desktop.
Remove Yoast Metabox for Everyone Except Admins
// Removes the Yoast Metabox for Roles other then Admins
// Returns true if user has specific role
function check_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) )
$user = get_userdata( $user_id );
else
$user = wp_get_current_user();
if ( empty( $user ) )
return false;
return in_array( $role, (array) $user->roles );
}
// Disable WordPress SEO meta box for all roles other than administrator and seo
function wpse_init(){
if( !( check_user_role( 'seo' ) || check_user_role( 'administrator' )) ) {
// Remove page analysis columns from post lists, also SEO status on post editor
add_filter( 'wpseo_use_page_analysis', '__return_false' );
// Remove Yoast meta boxes
add_action( 'add_meta_boxes', 'disable_seo_metabox', 100000 );
}
}
add_action('init', 'wpse_init');
function disable_seo_metabox() {
remove_meta_box( 'wpseo_meta', 'post', 'normal' );
remove_meta_box( 'wpseo_meta', 'page', 'normal' );
}
@gavin310
Copy link

Thanks!

@omniacode
Copy link
Author

@gavin310 -

Just FYI, haven't tested this recently so obviously use at your own risk. Let me know how it goes.

@gavin310
Copy link

@omniacode My use case was much simpler, but it worked great:

add_action( 'add_meta_boxes', function() {
	global $post;
	if ( $post && 43 === $post->ID ) {
		// Hide meta box from portal page
		remove_meta_box( 'wpseo_meta', 'page', 'normal' );
	}
}, 9999 );

@omniacode
Copy link
Author

@gavin310 -

As reported here, there is an error showing up in console and confirmed on one of our dev sites. When I have time we will try and troubleshoot it and update the code.

https://wordpress.org/support/topic/removing-yoast-meta-box-from-post-edit-page/

@kozmoz
Copy link

kozmoz commented Aug 15, 2022

I'm a little late to the party, but experienced the same error today on Wordpress 6.0.1 while disabling "wpseo_meta" Yoast SEO.

Fixed it by a script based on: https://www.intechgrity.com/remove-external-scripts-styles-from-wordpress-admin-page/#

function remove_yoast_assets() {
  $assets = [
    'styles' => wp_styles(),
    'scripts' => wp_scripts(),
  ];

  foreach ($assets as $type => $asset) {
    foreach ($asset->registered as $handle => $dep) {
      $id = $dep->handle;
      $src = $dep->src;
      if (strpos("$src|$id", 'yoast-') !== FALSE) {
        if ('scripts' === $type) {
          wp_dequeue_script($handle);
        } else {
          wp_dequeue_style($handle);
        }
      }
    }
  }
}

@Liam-Nothing
Copy link

Really thx !

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