0

How can I properly prevent scripts and styles from loading on dashboard for some user role? My users need only access to one plugin and that's the only thing they see on dashboard. How can I dequeue scripts for them?

1

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.

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' );

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' );
  • Thank you, yes :) But still I need to get rid of many scripts and styles :/ – Rafal Nov 24 '16 at 19:00
  • Why :/ ? If you need to dequeue then dequeue the scripts. Maybe you could list a few of the ones you're interested in removing – jgraup Nov 24 '16 at 19:20
  • will .... wp_dequeue_script( '' ); wp_deregister_script( '' ); }}add_action( 'wp_print_scripts', 'de_script', 100 ); work for dashboard scripts? What about styles? – Rafal Nov 24 '16 at 19:35
  • Added an example but it hasn't been tested. Going to enjoy Turkey Day but this should get you somewhere – jgraup Nov 24 '16 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.