返回介绍

get_current_screen()

发布于 2017-09-10 23:11:18 字数 5655 浏览 922 评论 0 收藏 0

get_current_screen()

Get the current screen object


description


返回值

(WP_Screen|null) Current screen object or null when screen not defined.


源代码

File: wp-admin/includes/screen.php

function get_current_screen() {
	global $current_screen;

	if ( ! isset( $current_screen ) )
		return null;

	return $current_screen;
}

更新日志

Versiondescription
3.1.0Introduced.

相关函数

Used By

  • wp-admin/includes/ajax-actions.php: wp_ajax_search_install_plugins()
  • wp-admin/includes/ajax-actions.php: wp_ajax_search_plugins()
  • wp-admin/includes/class-wp-screen.php: WP_Screen::render_view_mode()
  • wp-admin/includes/screen.php: add_screen_option()
  • wp-admin/includes/class-wp-screen.php: WP_Screen::get()
  • wp-admin/includes/deprecated.php: screen_layout()
  • wp-admin/includes/deprecated.php: screen_options()
  • wp-admin/includes/deprecated.php: screen_meta()
  • wp-admin/includes/dashboard.php: wp_add_dashboard_widget()
  • wp-admin/includes/dashboard.php: wp_dashboard()
  • wp-admin/includes/dashboard.php: wp_dashboard_setup()
  • wp-admin/includes/list-table.php: _get_list_table()
  • wp-admin/includes/template.php: iframe_header()
  • wp-admin/includes/template.php: add_meta_box()
  • wp-admin/includes/template.php: do_meta_boxes()
  • wp-admin/includes/template.php: remove_meta_box()
  • wp-admin/includes/template.php: do_accordion_sections()
  • wp-admin/includes/meta-boxes.php: post_comment_meta_box()
  • wp-admin/includes/meta-boxes.php: page_attributes_meta_box()
  • wp-admin/custom-header.php: Custom_Image_Header::help()
  • wp-admin/custom-background.php: Custom_Background::admin_load()
  • wp-includes/functions.php: wp_auth_check_load()
  • wp-includes/admin-bar.php: wp_admin_bar_edit_menu()
  • Show 18 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by Codex

    Another Example:

    
    <?php
    
    add_action( 'current_screen', 'wpdocs_this_screen' );
    
    /**
     * Run code on the admin widgets page
     */
    function wpdocs_this_screen() {
        $currentScreen = get_current_screen();
        if( $currentScreen->id === "widgets" ) {
            // Run some code, only on the admin widgets page
        }
    }
    
    ?>
    
    
  2. This function is useful to figure out which screen you’re on in the Dashboard,

    
    add_action('trashed_post', 'trash_wpcrm_contact');
    function trash_wpcrm_contact($post_id){
        $screen = get_current_screen();
        if('wpcrm-contact' != $screen->post_type){ //this is not our custom post, so let's exit
          return;
        }
        ....
    }
    

    Another useful attribute of the WP_Screen object returned by this function is the $screen->base attribute which is set to 'post' for any custom post edit screen, and is set to 'edit' for the custom post admin table page, which is very handy when loading custom css/scripts for additional function on those pages.

    Default Usage
    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', 'wpdocs_admin_add_page');
    
    /**
     * Add an admin page
     */
    function wpdocs_admin_add_page() {
    	global $wpdocs_admin_page;
    	$wpdocs_admin_page = add_options_page(__('Wpdocs Admin Page', 'wpdocs_textdomain'),
    		__('Wpdocs Admin Page', 'wpdocs_textdomain'),
    		'manage_options', 'wpdocs_textdomain', 'wpdocs_admin_page');
    
    	// Adds my_help_tab when my_admin_page loads
    	add_action('load-'.$wpdocs_admin_page, 'wpdocs_admin_add_help_tab');
    }
    
    /**
     * Add a contextual help tab to the Wpdocs Admin Page
     */
    function wpdocs_admin_add_help_tab () {
        global $wpdocs_admin_page;
        $screen = get_current_screen();
    
        /*
         * Check if current screen is Wpdocs Admin Page
         * Don't add help tab if it's not
         */
        if ( $screen->id != $wpdocs_admin_page )
            return;
    
        // Add my_help_tab if current screen is My Admin Page
        $screen->add_help_tab( array(
            'id' => 'wpdocs_help_tab',
            'title' => __('Wpdocs Help Tab'),
            'content' => '<p>'
    		. __( 'Descriptive content that will show in Wpdocs Help Tab body goes here.', 'wpdocs_textdomain' )
    		. '</p>',
        ) );
    }
    ?>
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文