Last updated on September 25, 2023

Disable Specific Widgets in the WordPress Dashboard

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Disable dashboard widgets.

To disable specific widgets in the WordPress dashboard, you can use the wp_dashboard_setup action hook along with the remove_meta_box() function. This allows you to remove unwanted widgets from the dashboard screen.

Here’s an example code snippet that demonstrates how to disable the “Quick Draft” widget in the dashboard:

function wpsnippets_disable_quick_draft_widget() {
    remove_meta_box( 'dashboard_quick_draft', 'dashboard', 'side' );
}
add_action( 'wp_dashboard_setup', 'wpsnippets_disable_quick_draft_widget' );

In this example, we define a custom function wpsnippets_disable_quick_draft_widget() that uses the remove_meta_box() function to remove the “Quick Draft” widget. The first parameter of remove_meta_box() is the ID of the widget, which is 'dashboard_quick_draft' in this case. The second parameter is the dashboard page where the widget is located, which is 'dashboard'. The third parameter specifies the widget’s context, which is 'side' for a sidebar widget.

You can modify this code snippet to disable other widgets by changing the widget ID and the dashboard page as needed.

Examples

Example 1: Disable specific widgets in the WordPress dashboard for non-admin users

This use case demonstrates how to disable specific widgets in the WordPress dashboard for non-admin users. The code example uses the wp_dashboard_setup action hook to remove the desired widgets using the remove_meta_box() function.

function wpsnippets_disable_dashboard_widgets() {
    if ( ! current_user_can( 'manage_options' ) ) {
        remove_meta_box( 'dashboard_widget_id', 'dashboard', 'normal' );
    }
}
add_action( 'wp_dashboard_setup', 'wpsnippets_disable_dashboard_widgets' );

Example 2: Disable specific widgets in the WordPress dashboard for specific user roles

This use case shows how to disable specific widgets in the WordPress dashboard for specific user roles. The code example uses the wp_dashboard_setup action hook and checks the user’s role using the get_role() function before removing the desired widgets.

function wpsnippets_disable_dashboard_widgets() {
    $user = wp_get_current_user();
    $user_roles = $user->roles;

    if ( in_array( 'editor', $user_roles ) ) {
        remove_meta_box( 'dashboard_widget_id', 'dashboard', 'normal' );
    }
}
add_action( 'wp_dashboard_setup', 'wpsnippets_disable_dashboard_widgets' );

Example 3: Disable specific widgets in the WordPress dashboard based on user capabilities

This use case demonstrates how to disable specific widgets in the WordPress dashboard based on user capabilities. The code example uses the wp_dashboard_setup action hook and checks the user’s capabilities using the current_user_can() function before removing the desired widgets.

function wpsnippets_disable_dashboard_widgets() {
    if ( ! current_user_can( 'edit_posts' ) ) {
        remove_meta_box( 'dashboard_widget_id', 'dashboard', 'normal' );
    }
}
add_action( 'wp_dashboard_setup', 'wpsnippets_disable_dashboard_widgets' );

In all three examples, the wp_dashboard_setup action hook is used to execute the code when the dashboard page is loaded. The remove_meta_box() function is then used to remove the specified widget from the dashboard. The conditionals (current_user_can(), in_array(), or ! current_user_can()) are used to determine whether the widget should be disabled based on the user’s role, capabilities, or lack of admin privileges.

Last updated on September 25, 2023. Originally posted on October 4, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *