限制用户角色在仪表板上的脚本和样式

时间:2016-11-24 作者:Rafal

如何正确防止在某些用户角色的仪表板上加载脚本和样式?我的用户只需要访问一个插件,这是他们在仪表板上看到的唯一东西。如何为它们退出脚本队列?

1 个回复
最合适的回答,由SO网友:jgraup 整理而成

安装Query Monitor 检查哪些脚本以及它们为您的角色使用的挂钩。请记住,这些脚本可能具有依赖性。

您可能应该从Dashboard Widgets API.

仪表板上默认小部件的名称:

// 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\']
remove all 基于用户角色的小部件:

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\' );
要将脚本出列,您可以结合使用以下功能:


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

相关推荐

来自[JOB_Dashboard]页面的WP作业管理器要素作业

我正在使用WP Job Manager 插件。我想在[job\\u dashboard]页面上创建一个链接,允许用户对自己的工作进行特色化。我已将此代码添加到作业仪表板。php:echo \'<li><a href=\"\' . update_post_meta( $job->ID, \'_featured\', 1 ) . \'\">Featured</a></li>\'; 但这是我所有的工作,而不是我点击链接的那个。我怎样才能使我点击的那