Skip to main content
added 1783 characters in body
Source Link
jgraup
  • 9.8k
  • 3
  • 32
  • 68

Install Query Monitor to check which scripts and what hook they are using for your role. Keep in mind these scripts may have dependencies.


You should probably start with the Dashboard Widgets API.

function remove_dashboard_meta() {
    
    // get the current user
    $user = wp_get_current_user();

    // define roles that cannot see the widgets
    $blacklisted_roles = array('subscriber');

    // remove if the current user has a blacklisted role
    if( array_intersect($blacklisted_roles, $user->roles ) ) {
        
        remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');//since 3.8
    }
}
add_action( 'admin_init', 'remove_dashboard_meta' );

To dequeue scripts you can use a combination of these functions:


function remove_admin_scripts( ) {
//    if ( 'edit.php' != $hook ) {
//        return;
//    }

    // get the current user
    $user = wp_get_current_user();

    // define roles that cannot see the widgets
    $blacklisted_roles = array('subscriber');

    // get out of this function if the blacklisted roles are now found
    if( ! array_intersect($blacklisted_roles, $user->roles ) ) {
        return;
    }

    $jquery_ui = array (
        "jquery-ui-widget",
        "jquery-ui-mouse",
        "jquery-ui-accordion",
        "jquery-ui-autocomplete",
        "jquery-ui-slider",
        "jquery-ui-tabs",
        "jquery-ui-draggable",
        "jquery-ui-droppable",
        "jquery-ui-selectable",
        "jquery-ui-position",
        "jquery-ui-datepicker",
        "jquery-ui-resizable",
        "jquery-ui-dialog",
        "jquery-ui-button",
    );
    foreach ( $jquery_ui as $script ) {
        wp_deregister_script( $script );
    }
}
add_action( 'admin_enqueue_scripts', 'remove_admin_scripts' );

You should probably start with the Dashboard Widgets API.

function remove_dashboard_meta() {
    
    // get the current user
    $user = wp_get_current_user();

    // define roles that cannot see the widgets
    $blacklisted_roles = array('subscriber');

    // remove if the current user has a blacklisted role
    if( array_intersect($blacklisted_roles, $user->roles ) ) {
        
        remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');//since 3.8
    }
}
add_action( 'admin_init', 'remove_dashboard_meta' );

Install Query Monitor to check which scripts and what hook they are using for your role. Keep in mind these scripts may have dependencies.


You should probably start with the Dashboard Widgets API.

function remove_dashboard_meta() {
    
    // get the current user
    $user = wp_get_current_user();

    // define roles that cannot see the widgets
    $blacklisted_roles = array('subscriber');

    // remove if the current user has a blacklisted role
    if( array_intersect($blacklisted_roles, $user->roles ) ) {
        
        remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');//since 3.8
    }
}
add_action( 'admin_init', 'remove_dashboard_meta' );

To dequeue scripts you can use a combination of these functions:


function remove_admin_scripts( ) {
//    if ( 'edit.php' != $hook ) {
//        return;
//    }

    // get the current user
    $user = wp_get_current_user();

    // define roles that cannot see the widgets
    $blacklisted_roles = array('subscriber');

    // get out of this function if the blacklisted roles are now found
    if( ! array_intersect($blacklisted_roles, $user->roles ) ) {
        return;
    }

    $jquery_ui = array (
        "jquery-ui-widget",
        "jquery-ui-mouse",
        "jquery-ui-accordion",
        "jquery-ui-autocomplete",
        "jquery-ui-slider",
        "jquery-ui-tabs",
        "jquery-ui-draggable",
        "jquery-ui-droppable",
        "jquery-ui-selectable",
        "jquery-ui-position",
        "jquery-ui-datepicker",
        "jquery-ui-resizable",
        "jquery-ui-dialog",
        "jquery-ui-button",
    );
    foreach ( $jquery_ui as $script ) {
        wp_deregister_script( $script );
    }
}
add_action( 'admin_enqueue_scripts', 'remove_admin_scripts' );
Source Link
jgraup
  • 9.8k
  • 3
  • 32
  • 68

You should probably start with the Dashboard Widgets API.

The names of the default widgets on the dashboard:

// Main column (left):
$wp_meta_boxes['dashboard']['normal']['high']['dashboard_browser_nag']
$wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']
$wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']

// Side Column (right):
$wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']
$wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']

To remove all widgets based on user role:

function remove_dashboard_meta() {
    
    // get the current user
    $user = wp_get_current_user();

    // define roles that cannot see the widgets
    $blacklisted_roles = array('subscriber');

    // remove if the current user has a blacklisted role
    if( array_intersect($blacklisted_roles, $user->roles ) ) {
        
        remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');//since 3.8
    }
}
add_action( 'admin_init', 'remove_dashboard_meta' );