Change WordPress user roles and capabilities › Forums › New Feature Request › restrict/hide Gutenberg blocks by role › Reply To: restrict/hide Gutenberg blocks by role
01/01/2021 at 09:20
#7232
Vladimir
Keymaster
Hi Wolfgang,
You can try this version:
add_filter( 'allowed_block_types', 'misha_allowed_block_types', 10, 2 );
function misha_allowed_block_types( $allowed_blocks, $post ) {
$user = wp_get_current_user();
if ( in_array('administrator', $user->roles ) ) {
// Do not change the list of default blocks for user with administrator role
return $allowed_blocks;
}
if ( in_array('author', $user->roles ) ) {
// Customize the list of allowed blockes for user with role author
$allowed_blocks = array(
'core/image',
'core/paragraph',
'core/heading',
'core/list'
);
return $allowed_blocks;
}
return $allowed_blocks;
}