这取决于您是否要将用户重定向到您应该使用的init 钩子,因为在该钩子之前没有发送输出或标头。或者,如果您想显示一条漂亮的“您没有访问此页面的权限”消息,则可以使用wp_head 行动挂钩:
//display meassage
add_action(\'admin_head\',\'my_restrict_access\');
function my_restrict_access_meassage(){
    global $pagenow;
    if ($pagenow == \'upload.php\' && !current_user_can( \'upload_files\' )){
        echo \'<div class="wrap"><br />
            <div id="message" class="error">You Dont have the right permissions to access this page</div>
        </div>\';
        exit();
    }
}
//or redirect
add_action(\'init\',\'my_restrict_redirect\');
function my_restrict_redirect(){
    global $pagenow;
    if (!is_admin())
        return \'\';
    if ($pagenow == \'upload.php\' && !current_user_can( \'upload_files\' )){
        wp_redirect( home_url() );
        die();
    }
}