get current screen——获取仪表盘中当前页面信息

函数描述

该函数返回一个包含当前页面 ID 的对象,如:base, post type, 或 taxonomy, 以及其他类型的名称

使用方法

<?php $screen = get_current_screen(); ?>

参数

使用限制

该函数在很多管理页面中都有定义,但并不是所有页面,这会导致有时候 is_admin() will 返回真,而调用 get_current_screen() 却遇到了致命错误,因为这个函数在该也发没有定义,一个已知的页面是 wp-admin/customize.php。

我们应该在 admin_init 初始化之后调用,否则将返回 null。

返回值

返回一个 WP_Screen 对象,调用不当是返回 null。

返回对象包括以下字段:

id
(string) 当前页面的唯一 ID。
action
(string)  该页面的操作,如 *-new.php 页面的操作为’add’, 其他情况下为空。
base
(string) 该页面的基础类型,如 ‘post-new.php’ 的基础类型为 ‘post’。
parent_base
(string) 该页面的父级,The base menu parent. This is derived from $parent_file by removing the query string and any .php extension. For example, parent_file values of ‘edit.php?post_type=page’ and ‘edit.php?post_type=post’ have a parent_base of ‘edit’
parent_file
(string) The parent_file for the screen per the admin menu system. Some parent_file values are ‘edit.php?post_type=page’, ‘edit.php’, and ‘options-general.php’
post_type
(string) The post type associated with the screen, if any. For example, the ‘edit.php?post_type=page’ screen has a post type of ‘page’
taxonomy
(string) The taxonomy associated with the screen, if any. For example, the ‘edit-tags.php?taxonomy=category’ screen has a taxonomy of ‘category’

Example return value on a post edit page (as of WP version 3.3.1):

WP_Screen Object
(
    [action] => 
    [base] => post
    [id] => post
    [is_network] => 
    [is_user] => 
    [parent_base] => edit
    [parent_file] => edit.php
    [post_type] => post
    [taxonomy] => 
    ...
    (private fields)
)

使用示例

默认使用

This example shows how you would add contextual help to an admin page you’ve created with the add_options_page()function. Here, we assume that your admin page has a slug of ‘my_admin_page’ and exists under the Options tab.

The get_current_screen() function is used in this example.

<?php 
add_action('admin_menu', 'my_admin_add_page');
function my_admin_add_page() {
    global $my_admin_page;
    $my_admin_page = add_options_page(__('My Admin Page', 'map'), __('My Admin Page', 'map'), 'manage_options', 'map', 'my_admin_page');

    // Adds my_help_tab when my_admin_page loads
    add_action('load-'.$my_admin_page, 'my_admin_add_help_tab');
}

function my_admin_add_help_tab () {
    global $my_admin_page;
    $screen = get_current_screen();

    /*
     * Check if current screen is My Admin Page
     * Don't add help tab if it's not
     */
    if ( $screen->id != $my_admin_page )
        return;

    // Add my_help_tab if current screen is My Admin Page
    $screen->add_help_tab( array(
        'id'	=> 'my_help_tab',
        'title'	=> __('My Help Tab'),
        'content'	=> '<p>' . __( 'Descriptive content that will show in My Help Tab-body goes here.' ) . '</p>',
    ) );
}
?>

其他示例:

<?php
add_action( 'current_screen', 'thisScreen' );

function thisScreen() {
    $currentScreen = get_current_screen();
    if( $currentScreen->id === "widgets" ) {
        // Run some code, only on the admin widgets page
    }
}
?>
支付确认
需要支付
¥0
二维码刷新中