Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save vbaimas/d7f992ad0fb8ff31553e736e6e2b05b3 to your computer and use it in GitHub Desktop.

Select an option

Save vbaimas/d7f992ad0fb8ff31553e736e6e2b05b3 to your computer and use it in GitHub Desktop.

Revisions

  1. vbaimas created this gist Jan 17, 2020.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    // Returns true if user has specific role
    function {your_prefix}_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 Yoast SEO meta box for all roles other than administrator
    function {your_prefix}_wpse_init(){
    if( !({your_prefix}_check_user_role('seo') || {your_prefix}_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', '{your_prefix}_disable_seo_metabox', 100000);
    }
    }
    add_action('init', '{your_prefix}_wpse_init');

    function {your_prefix}_disable_seo_metabox(){
    remove_meta_box('wpseo_meta', '{your_custom_post_type}', 'normal'); // Remove from custom post type
    remove_meta_box('wpseo_meta', 'post', 'normal'); // Remove from post
    remove_meta_box('wpseo_meta', 'page', 'normal'); // Remove from page
    }