WordPress SEO by Yoast: Hide Meta Boxes in Posts for Non-admins

I have multi-author website and I am not very comfortable for allowing all members to enter SEO details in the posts they are publishing. I would like this would be visible only to Administrator of the website. Any ideas?

Related posts

Leave a Reply

5 comments

  1. It didn’t say in the API docs on the Yoast SEO plugin site what the ID was and I don’t have a copy of Yoast at installed at disposal, but according to yoas-plugin-dir/admin/class-metabox.php line 144, the meta_box registered is;

    add_meta_box( 'wpseo_meta', ...etc ); ...
    

    Which is hooked onto add_meta_boxes hook on line 32 of the same file,

    add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
    

    Of course you could get the ID from the meta box itself on the post edit screen… Anyway.

    You can do the following,

    add_action('add_meta_boxes', 'yoast_is_toast', 99);
    function yoast_is_toast(){
        //capability of 'manage_plugins' equals admin, therefore if NOT administrator
        //hide the meta box from all other roles on the following 'post_type' 
        //such as post, page, custom_post_type, etc
        if (!current_user_can('activate_plugins')) {
            remove_meta_box('wpseo_meta', 'post_type', 'normal');
        }
    }
    

    …where post type is the post type you wish to apply this restriction too, such as post or a custom post type one or more!

    Should do the trick.

    update: manage_plugins should have been activate_plugins – ammended.

  2. Try pasting this in a plugin or theme functions.php file, as per the WordPress SEO plugin (API Docs), by Yoast.

    if(function_exists('wpseo_use_page_analysis') && !current_user_can('administrator')){
        add_filter('wpseo_use_page_analysis', '__return_false');
    }
    

    To avoid any errors… This checks to make sure the function exists, before attempting to hide stuff, allowing you to deactivate the plugin, and not throw errors.

    You may need to hook into init for this to properly work, which could be done so like this:

    function wpse_init(){
        if(function_exists('wpseo_use_page_analysis') && !current_user_can('administrator')){
            add_filter('wpseo_use_page_analysis', '__return_false');
        }   
    }
    add_action('init', 'wpse_init');
    
  3. You can remove it using the remove_meta_box function.

    if ( ! current_user_can( 'edit_pages' ) ) {
        add_action( 'add_meta_boxes', 'my_remove_wp_seo_meta_box', 100000 );
    }
    
    function my_remove_wp_seo_meta_box() {
        remove_meta_box( 'wpseo_meta', 'post', 'normal' );
    }
    

    Notice the 100000 in the add_action, this makes sure that this is done after the WP SEO metabox has been hooked in.

  4. Well there is a setting option on the WordPress SEO by Yoast for disabling the advanced part of WordPress SEO settings for non admins. If you want to completely disable the box then you can use other plugins to remove post editor page elements for non admins, e.g. removing the WordPress SEO box from the post editor for non admins.