我认为通过使用[get_current_screen()][1]
并从中读取属性WP_Screen 根据需要返回对象。就你而言,我想你需要$screen->base
或者也许$screen->id
. 根据WordPress Codex:
id(字符串)屏幕的唯一id
和
base(string)屏幕的基本类型。例如,对于页面“post new”。php“基础是”post“。
例如,Yoast SEO仪表板位于admin上。php?页面=wpseo\\u仪表板和$screen->base property
对于此页为\'toplevel_page_wpseo_dashboard
\'
下面是我几周前工作的一个例子。
/**
* Disallow Admin pages for non-superadmin users on current_screen hook.
* A fallback for when restricting access is not possible by manage_admin_access_on_init
*
* @since 0.1.0
*/
public function wpse_283875_manage_admin_access_on_screen() {
// a list of restricted pages base properties from get_current_screen
$restricted_screens = array(
\'formularios_page_gf_help\',
\'toplevel_page_genesis\',
\'genesis_page_genesis-translations\',
\'genesis_page_genesis-import-export\',
\'genesis_page_genesis-simple-edits\',
\'media_page_wp-smush-bulk\',
);
// get current screen
$screen = get_current_screen();
// if there\'s no $screen object, we are in customizer, bail out
if ( is_null($screen) ){
return;
}
// get screen->base
$screen_base = $screen->base;
// compare screen->base with each restricted screen
foreach ($restricted_screens as $restricted_screen) {
// if user can\'t manage network and screen base is restricted
if (!current_user_can(\'manage_network\') && $screen_base == $restricted_screen) {
//get out!
wp_redirect(admin_url());
// stop execution
exit;
}
}
}
// hook to current_screen action with priority 5 to execut ASAP
add_action(\'current_screen\',\'manage_admin_access_on_screen\', 5);
如果需要检查screen对象,可以在开发环境中使用类似的内容并阅读HTML源代码:
function debug_admin_screen(){
$screen = get_current_screen();
echo \'<!--<pre>\' . print_r($screen, TRUE) . \'</pre>-->\';
}
add_action(\'current_screen\', \'debug_admin_screen\', 100);