Skip to content

Instantly share code, notes, and snippets.

@itsHall
Forked from omniacode/wp-remove-yoast.php
Last active October 7, 2021 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsHall/c3830b99e32f5ebce0536a761d3d2574 to your computer and use it in GitHub Desktop.
Save itsHall/c3830b99e32f5ebce0536a761d3d2574 to your computer and use it in GitHub Desktop.
Remove Yoast Metabox for Everyone Except Admins
<?php
// 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' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment