2 approaches to this. Compare them and pick what fits your setup.
This snippet removes all admin notices and dashboard notifications for users with the Editor role in the WordPress admin panel. It helps create a cleaner and distraction-free admin experience by hiding plugin, theme, and core notices from editor-level users.
/**
* Hide all WordPress admin notices for editor users.
*
* Removes plugin, theme, and core admin notices from the dashboard
* for users who have the Editor capability to provide a cleaner
* admin experience.
*
*/
function sj_hide_admin_notices_for_editors() {
// Check if the current logged-in user has editor access.
if ( current_user_can( 'editor' ) ) {
// Remove all admin notice actions.
remove_all_actions( 'admin_notices' );
remove_all_actions( 'all_admin_notices' );
}
}
add_action( 'admin_init', 'sj_hide_admin_notices_for_editors' );
Here's a lightweight WordPress code snippet that lets you hide any Gutenberg block based on whether the visitor is logged in or not.
Simply add one of these CSS classes to a block:
-
hide-for-guests→ Visible only to logged-in users. -
hide-for-members→ Visible only to logged-out visitors.
No plugin required.
Step 1: Add to functions.php
Step 2: Use It in Gutenberg
-
Select any block.
-
Open Advanced → Additional CSS class(es).
-
Add one of these classes:
|
Class |
Visibility |
|---|---|
|
|
Logged-in users only |
|
|
Logged-out visitors only |
Example
A Download Button with the class hide-for-guests will only appear for members who are logged in.
A Sign Up call-to-action with hide-for-members will only appear to visitors who haven't logged in yet.
Why this approach?
-
No extra plugin.
-
Works with any Gutenberg block.
-
Uses WordPress's
render_blockfilter. -
Keeps hidden content out of the rendered HTML, making it more secure than hiding it with CSS alone.
/**
* Hide Gutenberg blocks based on user login status.
*
* Classes:
* - hide-for-guests = Only logged-in users can see it.
* - hide-for-members = Only logged-out visitors can see it.
*/
function txwp_block_visibility_by_role( $block_content, $block ) {
if ( empty( $block['attrs']['className'] ) ) {
return $block_content;
}
$classes = $block['attrs']['className'];
// Hide from guests.
if ( strpos( $classes, 'hide-for-guests' ) !== false && ! is_user_logged_in() ) {
return '';
}
// Hide from logged-in users.
if ( strpos( $classes, 'hide-for-members' ) !== false && is_user_logged_in() ) {
return '';
}
return $block_content;
}
add_filter( 'render_block', 'txwp_block_visibility_by_role', 10, 2 );