3 * WordPress Administration Screen API.
6 * @subpackage Administration
10 * Get the column headers for a screen
14 * @param string|WP_Screen $screen The screen you want the headers for
15 * @return array Containing the headers in the format id => UI String
17 function get_column_headers( $screen ) {
18 if ( is_string( $screen ) )
19 $screen = convert_to_screen( $screen );
21 static $column_headers = array();
23 if ( ! isset( $column_headers[ $screen->id ] ) )
24 $column_headers[ $screen->id ] = apply_filters( 'manage_' . $screen->id . '_columns', array() );
26 return $column_headers[ $screen->id ];
30 * Get a list of hidden columns.
34 * @param string|WP_Screen $screen The screen you want the hidden columns for
37 function get_hidden_columns( $screen ) {
38 if ( is_string( $screen ) )
39 $screen = convert_to_screen( $screen );
41 return (array) get_user_option( 'manage' . $screen->id . 'columnshidden' );
45 * Prints the meta box preferences for screen meta.
49 * @param string|WP_Screen $screen
51 function meta_box_prefs( $screen ) {
52 global $wp_meta_boxes;
54 if ( is_string( $screen ) )
55 $screen = convert_to_screen( $screen );
57 if ( empty($wp_meta_boxes[$screen->id]) )
60 $hidden = get_hidden_meta_boxes($screen);
62 foreach ( array_keys($wp_meta_boxes[$screen->id]) as $context ) {
63 foreach ( array_keys($wp_meta_boxes[$screen->id][$context]) as $priority ) {
64 foreach ( $wp_meta_boxes[$screen->id][$context][$priority] as $box ) {
65 if ( false == $box || ! $box['title'] )
67 // Submit box cannot be hidden
68 if ( 'submitdiv' == $box['id'] || 'linksubmitdiv' == $box['id'] )
71 echo '<label for="' . $box_id . '-hide">';
72 echo '<input class="hide-postbox-tog" name="' . $box_id . '-hide" type="checkbox" id="' . $box_id . '-hide" value="' . $box_id . '"' . (! in_array($box_id, $hidden) ? ' checked="checked"' : '') . ' />';
73 echo "{$box['title']}</label>\n";
80 * Get Hidden Meta Boxes
84 * @param string|WP_Screen $screen Screen identifier
85 * @return array Hidden Meta Boxes
87 function get_hidden_meta_boxes( $screen ) {
88 if ( is_string( $screen ) )
89 $screen = convert_to_screen( $screen );
91 $hidden = get_user_option( "metaboxhidden_{$screen->id}" );
93 $use_defaults = ! is_array( $hidden );
95 // Hide slug boxes by default
96 if ( $use_defaults ) {
98 if ( 'post' == $screen->base ) {
99 if ( 'post' == $screen->post_type || 'page' == $screen->post_type )
100 $hidden = array('slugdiv', 'trackbacksdiv', 'postcustom', 'postexcerpt', 'commentstatusdiv', 'commentsdiv', 'authordiv', 'revisionsdiv');
102 $hidden = array( 'slugdiv' );
104 $hidden = apply_filters( 'default_hidden_meta_boxes', $hidden, $screen );
107 return apply_filters( 'hidden_meta_boxes', $hidden, $screen, $use_defaults );
111 * Register and configure an admin screen option
115 * @param string $option An option name.
116 * @param mixed $args Option-dependent arguments.
118 function add_screen_option( $option, $args = array() ) {
119 $current_screen = get_current_screen();
121 if ( ! $current_screen )
124 $current_screen->add_option( $option, $args );
128 * Displays a screen icon.
130 * @uses get_screen_icon()
133 * @param string|WP_Screen $screen Optional. Accepts a screen object (and defaults to the current screen object)
134 * which it uses to determine an icon HTML ID. Or, if a string is provided, it is used to form the icon HTML ID.
136 function screen_icon( $screen = '' ) {
137 echo get_screen_icon( $screen );
141 * Gets a screen icon.
145 * @param string|WP_Screen $screen Optional. Accepts a screen object (and defaults to the current screen object)
146 * which it uses to determine an icon HTML ID. Or, if a string is provided, it is used to form the icon HTML ID.
147 * @return string HTML for the screen icon.
149 function get_screen_icon( $screen = '' ) {
150 if ( empty( $screen ) )
151 $screen = get_current_screen();
152 elseif ( is_string( $screen ) )
157 if ( empty( $icon_id ) ) {
158 if ( ! empty( $screen->parent_base ) )
159 $icon_id = $screen->parent_base;
161 $icon_id = $screen->base;
163 if ( 'page' == $screen->post_type )
164 $icon_id = 'edit-pages';
166 if ( $screen->post_type )
167 $class .= ' ' . sanitize_html_class( 'icon32-posts-' . $screen->post_type );
170 return '<div id="icon-' . esc_attr( $icon_id ) . '" class="' . $class . '"><br /></div>';
174 * Get the current screen object
178 * @return WP_Screen Current screen object
180 function get_current_screen() {
181 global $current_screen;
183 if ( ! isset( $current_screen ) )
186 return $current_screen;
190 * Set the current screen object
193 * @uses $current_screen
195 * @param mixed $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen,
196 * or an existing screen object.
198 function set_current_screen( $hook_name = '' ) {
199 WP_Screen::get( $hook_name )->set_current_screen();
203 * A class representing the admin screen.
208 final class WP_Screen {
210 * Any action associated with the screen. 'add' for *-add.php and *-new.php screens. Empty otherwise.
219 * The base type of the screen. This is typically the same as $id but with any post types and taxonomies stripped.
220 * For example, for an $id of 'edit-post' the base is 'edit'.
229 * The number of columns to display. Access with get_columns().
235 private $columns = 0;
238 * The unique ID of the screen.
247 * Whether the screen is in the network admin.
256 * Whether the screen is in the user admin.
265 * The base menu parent.
266 * This is derived from $parent_file by removing the query string and any .php extension.
267 * $parent_file values of 'edit.php?post_type=page' and 'edit.php?post_type=post' have a $parent_base of 'edit'.
276 * The parent_file for the screen per the admin menu system.
277 * Some $parent_file values are 'edit.php?post_type=page', 'edit.php', and 'options-general.php'.
286 * The post type associated with the screen, if any.
287 * The 'edit.php?post_type=page' screen has a post type of 'page'.
288 * The 'edit-tags.php?taxonomy=$taxonomy&post_type=page' screen has a post type of 'page'.
297 * The taxonomy associated with the screen, if any.
298 * The 'edit-tags.php?taxonomy=category' screen has a taxonomy of 'category'.
306 * The help tab data associated with the screen, if any.
312 private $_help_tabs = array();
315 * The help sidebar data associated with screen, if any.
321 private $_help_sidebar = '';
324 * Stores old string-based help.
326 private static $_old_compat_help = array();
329 * The screen options associated with screen, if any.
335 private $_options = array();
338 * The screen object registry.
344 private static $_registry = array();
347 * Stores the result of the public show_screen_options function.
353 private $_show_screen_options;
356 * Stores the 'screen_settings' section of screen options.
362 private $_screen_settings;
365 * Fetches a screen object.
370 * @param string $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen.
371 * Defaults to the current $hook_suffix global.
372 * @return WP_Screen Screen object.
374 public static function get( $hook_name = '' ) {
376 if ( is_a( $hook_name, 'WP_Screen' ) )
379 $post_type = $taxonomy = null;
380 $is_network = $is_user = false;
386 $id = $GLOBALS['hook_suffix'];
388 // For those pesky meta boxes.
389 if ( $hook_name && post_type_exists( $hook_name ) ) {
391 $id = 'post'; // changes later. ends up being $base.
393 if ( '.php' == substr( $id, -4 ) )
394 $id = substr( $id, 0, -4 );
396 if ( 'post-new' == $id || 'link-add' == $id || 'media-new' == $id || 'user-new' == $id ) {
397 $id = substr( $id, 0, -4 );
402 if ( ! $post_type && $hook_name ) {
403 if ( '-network' == substr( $id, -8 ) ) {
404 $id = substr( $id, 0, -8 );
406 } elseif ( '-user' == substr( $id, -5 ) ) {
407 $id = substr( $id, 0, -5 );
411 $id = sanitize_key( $id );
412 if ( 'edit-comments' != $id && 'edit-tags' != $id && 'edit-' == substr( $id, 0, 5 ) ) {
413 $maybe = substr( $id, 5 );
414 if ( taxonomy_exists( $maybe ) ) {
417 } elseif ( post_type_exists( $maybe ) ) {
423 $is_network = is_network_admin();
424 $is_user = is_user_admin();
427 if ( 'index' == $id )
432 // If this is the current screen, see if we can be more accurate for post types and taxonomies.
433 if ( ! $hook_name ) {
434 if ( isset( $_REQUEST['post_type'] ) )
435 $post_type = post_type_exists( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : false;
436 if ( isset( $_REQUEST['taxonomy'] ) )
437 $taxonomy = taxonomy_exists( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : false;
441 if ( isset( $_GET['post'] ) )
442 $post_id = (int) $_GET['post'];
443 elseif ( isset( $_POST['post_ID'] ) )
444 $post_id = (int) $_POST['post_ID'];
449 $post = get_post( $post_id );
451 $post_type = $post->post_type;
455 if ( null === $post_type && is_object_in_taxonomy( 'post', $taxonomy ? $taxonomy : 'post_tag' ) )
463 if ( null === $post_type )
468 if ( null === $post_type )
470 $id .= '-' . $post_type;
473 if ( null === $taxonomy )
474 $taxonomy = 'post_tag';
475 $id = 'edit-' . $taxonomy;
482 } elseif ( $is_user ) {
487 if ( isset( self::$_registry[ $id ] ) ) {
488 $screen = self::$_registry[ $id ];
489 if ( $screen === get_current_screen() )
492 $screen = new WP_Screen();
496 $screen->base = $base;
497 $screen->action = $action;
498 $screen->post_type = (string) $post_type;
499 $screen->taxonomy = (string) $taxonomy;
500 $screen->is_user = $is_user;
501 $screen->is_network = $is_network;
503 self::$_registry[ $id ] = $screen;
509 * Makes the screen object the current screen.
511 * @see set_current_screen()
514 function set_current_screen() {
515 global $current_screen, $taxnow, $typenow;
516 $current_screen = $this;
517 $taxnow = $this->taxonomy;
518 $typenow = $this->post_type;
519 do_action( 'current_screen', $current_screen );
528 private function __construct() {}
531 * Sets the old string-based contextual help for the screen.
533 * For backwards compatibility.
537 * @param WP_Screen $screen A screen object.
538 * @param string $help Help text.
540 static function add_old_compat_help( $screen, $help ) {
541 self::$_old_compat_help[ $screen->id ] = $help;
545 * Set the parent information for the screen.
546 * This is called in admin-header.php after the menu parent for the screen has been determined.
550 * @param string $parent_file The parent file of the screen. Typically the $parent_file global.
552 function set_parentage( $parent_file ) {
553 $this->parent_file = $parent_file;
554 list( $this->parent_base ) = explode( '?', $parent_file );
555 $this->parent_base = str_replace( '.php', '', $this->parent_base );
559 * Adds an option for the screen.
560 * Call this in template files after admin.php is loaded and before admin-header.php is loaded to add screen options.
564 * @param string $option Option ID
565 * @param mixed $args Option-dependent arguments.
567 public function add_option( $option, $args = array() ) {
568 $this->_options[ $option ] = $args;
572 * Gets the arguments for an option for the screen.
576 * @param string $option Option ID.
577 * @param mixed $key Optional. Specific array key for when the option is an array.
579 public function get_option( $option, $key = false ) {
580 if ( ! isset( $this->_options[ $option ] ) )
583 if ( isset( $this->_options[ $option ][ $key ] ) )
584 return $this->_options[ $option ][ $key ];
587 return $this->_options[ $option ];
591 * Gets the help tabs registered for the screen.
595 * @return array Help tabs with arguments.
597 public function get_help_tabs() {
598 return $this->_help_tabs;
602 * Gets the arguments for a help tab.
606 * @param string $id Help Tab ID.
607 * @return array Help tab arguments.
609 public function get_help_tab( $id ) {
610 if ( ! isset( $this->_help_tabs[ $id ] ) )
612 return $this->_help_tabs[ $id ];
616 * Add a help tab to the contextual help for the screen.
617 * Call this on the load-$pagenow hook for the relevant screen.
622 * - string - title - Title for the tab.
623 * - string - id - Tab ID. Must be HTML-safe.
624 * - string - content - Help tab content in plain text or HTML. Optional.
625 * - callback - callback - A callback to generate the tab content. Optional.
628 public function add_help_tab( $args ) {
635 $args = wp_parse_args( $args, $defaults );
637 $args['id'] = sanitize_html_class( $args['id'] );
639 // Ensure we have an ID and title.
640 if ( ! $args['id'] || ! $args['title'] )
643 // Allows for overriding an existing tab with that ID.
644 $this->_help_tabs[ $args['id'] ] = $args;
648 * Removes a help tab from the contextual help for the screen.
652 * @param string $id The help tab ID.
654 public function remove_help_tab( $id ) {
655 unset( $this->_help_tabs[ $id ] );
659 * Removes all help tabs from the contextual help for the screen.
663 public function remove_help_tabs() {
664 $this->_help_tabs = array();
668 * Gets the content from a contextual help sidebar.
672 * @return string Contents of the help sidebar.
674 public function get_help_sidebar() {
675 return $this->_help_sidebar;
679 * Add a sidebar to the contextual help for the screen.
680 * Call this in template files after admin.php is loaded and before admin-header.php is loaded to add a sidebar to the contextual help.
684 * @param string $content Sidebar content in plain text or HTML.
686 public function set_help_sidebar( $content ) {
687 $this->_help_sidebar = $content;
691 * Gets the number of layout columns the user has selected.
693 * The layout_columns option controls the max number and default number of
694 * columns. This method returns the number of columns within that range selected
695 * by the user via Screen Options. If no selection has been made, the default
696 * provisioned in layout_columns is returned. If the screen does not support
697 * selecting the number of layout columns, 0 is returned.
701 * @return int Number of columns to display.
703 public function get_columns() {
704 return $this->columns;
708 * Render the screen's help section.
710 * This will trigger the deprecated filters for backwards compatibility.
714 public function render_screen_meta() {
716 // Call old contextual_help_list filter.
717 self::$_old_compat_help = apply_filters( 'contextual_help_list', self::$_old_compat_help, $this );
719 $old_help = isset( self::$_old_compat_help[ $this->id ] ) ? self::$_old_compat_help[ $this->id ] : '';
720 $old_help = apply_filters( 'contextual_help', $old_help, $this->id, $this );
722 // Default help only if there is no old-style block of text and no new-style help tabs.
723 if ( empty( $old_help ) && ! $this->get_help_tabs() ) {
724 $default_help = apply_filters( 'default_contextual_help', '' );
726 $old_help = '<p>' . $default_help . '</p>';
730 $this->add_help_tab( array(
731 'id' => 'old-contextual-help',
732 'title' => __('Overview'),
733 'content' => $old_help,
737 $help_sidebar = $this->get_help_sidebar();
739 $help_class = 'hidden';
740 if ( ! $help_sidebar )
741 $help_class .= ' no-sidebar';
745 <div id="screen-meta" class="metabox-prefs">
747 <div id="contextual-help-wrap" class="<?php echo esc_attr( $help_class ); ?>">
748 <div id="contextual-help-back"></div>
749 <div id="contextual-help-columns">
750 <div class="contextual-help-tabs">
753 $class = ' class="active"';
754 foreach ( $this->get_help_tabs() as $tab ) :
755 $link_id = "tab-link-{$tab['id']}";
756 $panel_id = "tab-panel-{$tab['id']}";
759 <li id="<?php echo esc_attr( $link_id ); ?>"<?php echo $class; ?>>
760 <a href="<?php echo esc_url( "#$panel_id" ); ?>">
761 <?php echo esc_html( $tab['title'] ); ?>
771 <?php if ( $help_sidebar ) : ?>
772 <div class="contextual-help-sidebar">
773 <?php echo $help_sidebar; ?>
777 <div class="contextual-help-tabs-wrap">
779 $classes = 'help-tab-content active';
780 foreach ( $this->get_help_tabs() as $tab ):
781 $panel_id = "tab-panel-{$tab['id']}";
784 <div id="<?php echo esc_attr( $panel_id ); ?>" class="<?php echo $classes; ?>">
786 // Print tab content.
787 echo $tab['content'];
789 // If it exists, fire tab callback.
790 if ( ! empty( $tab['callback'] ) )
791 call_user_func_array( $tab['callback'], array( $this, $tab ) );
795 $classes = 'help-tab-content';
802 // Setup layout columns
804 // Back compat for plugins using the filter instead of add_screen_option()
805 $columns = apply_filters( 'screen_layout_columns', array(), $this->id, $this );
807 if ( ! empty( $columns ) && isset( $columns[ $this->id ] ) )
808 $this->add_option( 'layout_columns', array('max' => $columns[ $this->id ] ) );
810 if ( $this->get_option( 'layout_columns' ) ) {
811 $this->columns = (int) get_user_option("screen_layout_$this->id");
813 if ( ! $this->columns && $this->get_option( 'layout_columns', 'default' ) )
814 $this->columns = $this->get_option( 'layout_columns', 'default' );
816 $GLOBALS[ 'screen_layout_columns' ] = $this->columns; // Set the global for back-compat.
818 // Add screen options
819 if ( $this->show_screen_options() )
820 $this->render_screen_options();
824 if ( ! $this->get_help_tabs() && ! $this->show_screen_options() )
827 <div id="screen-meta-links">
828 <?php if ( $this->get_help_tabs() ) : ?>
829 <div id="contextual-help-link-wrap" class="hide-if-no-js screen-meta-toggle">
830 <a href="#contextual-help-wrap" id="contextual-help-link" class="show-settings"><?php _e( 'Help' ); ?></a>
833 if ( $this->show_screen_options() ) : ?>
834 <div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle">
835 <a href="#screen-options-wrap" id="show-settings-link" class="show-settings"><?php _e( 'Screen Options' ); ?></a>
842 public function show_screen_options() {
843 global $wp_meta_boxes;
845 if ( is_bool( $this->_show_screen_options ) )
846 return $this->_show_screen_options;
848 $columns = get_column_headers( $this );
850 $show_screen = ! empty( $wp_meta_boxes[ $this->id ] ) || $columns || $this->get_option( 'per_page' );
852 $this->_screen_settings = apply_filters( 'screen_settings', '', $this );
854 switch ( $this->id ) {
856 $this->_screen_settings = '<p><a id="access-on" href="widgets.php?widgets-access=on">' . __('Enable accessibility mode') . '</a><a id="access-off" href="widgets.php?widgets-access=off">' . __('Disable accessibility mode') . "</a></p>\n";
860 if ( $this->_screen_settings || $this->_options )
863 $this->_show_screen_options = apply_filters( 'screen_options_show_screen', $show_screen, $this );
864 return $this->_show_screen_options;
868 * Render the screen options tab.
872 public function render_screen_options() {
873 global $wp_meta_boxes, $wp_list_table;
875 $columns = get_column_headers( $this );
876 $hidden = get_hidden_columns( $this );
879 <div id="screen-options-wrap" class="hidden">
880 <form id="adv-settings" action="" method="post">
881 <?php if ( isset( $wp_meta_boxes[ $this->id ] ) || $this->get_option( 'per_page' ) || ( $columns && empty( $columns['_title'] ) ) ) : ?>
882 <h5><?php _e( 'Show on screen' ); ?></h5>
886 if ( isset( $wp_meta_boxes[ $this->id ] ) ) : ?>
887 <div class="metabox-prefs">
889 meta_box_prefs( $this );
891 if ( 'dashboard' === $this->id && current_user_can( 'edit_theme_options' ) ) {
892 if ( isset( $_GET['welcome'] ) ) {
893 $welcome_checked = empty( $_GET['welcome'] ) ? 0 : 1;
894 update_user_meta( get_current_user_id(), 'show_welcome_panel', $welcome_checked );
896 $welcome_checked = get_user_meta( get_current_user_id(), 'show_welcome_panel', true );
897 if ( 2 == $welcome_checked && wp_get_current_user()->user_email != get_option( 'admin_email' ) )
898 $welcome_checked = false;
900 echo '<label for="wp_welcome_panel-hide">';
901 echo '<input type="checkbox" id="wp_welcome_panel-hide"' . checked( (bool) $welcome_checked, true, false ) . ' />';
902 echo _x( 'Welcome', 'Welcome panel' ) . "</label>\n";
909 if ( ! empty( $columns['_title'] ) ) : ?>
910 <h5><?php echo $columns['_title']; ?></h5>
912 <div class="metabox-prefs">
914 $special = array('_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname');
916 foreach ( $columns as $column => $title ) {
917 // Can't hide these for they are special
918 if ( in_array( $column, $special ) )
920 if ( empty( $title ) )
923 if ( 'comments' == $column )
924 $title = __( 'Comments' );
925 $id = "$column-hide";
926 echo '<label for="' . $id . '">';
927 echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( !in_array($column, $hidden), true, false ) . ' />';
928 echo "$title</label>\n";
935 $this->render_screen_layout();
936 $this->render_per_page_options();
937 echo $this->_screen_settings;
940 <div><?php wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false ); ?></div>
947 * Render the option for number of columns on the page
951 function render_screen_layout() {
952 if ( ! $this->get_option('layout_columns') )
955 $screen_layout_columns = $this->get_columns();
956 $num = $this->get_option( 'layout_columns', 'max' );
959 <h5 class="screen-layout"><?php _e('Screen Layout'); ?></h5>
960 <div class='columns-prefs'><?php
961 _e('Number of Columns:');
962 for ( $i = 1; $i <= $num; ++$i ):
964 <label class="columns-prefs-<?php echo $i; ?>">
965 <input type='radio' name='screen_columns' value='<?php echo esc_attr( $i ); ?>'
966 <?php checked( $screen_layout_columns, $i ); ?> />
967 <?php echo esc_html( $i ); ?>
976 * Render the items per page option
980 function render_per_page_options() {
981 if ( ! $this->get_option( 'per_page' ) )
984 $per_page_label = $this->get_option( 'per_page', 'label' );
986 $option = $this->get_option( 'per_page', 'option' );
988 $option = str_replace( '-', '_', "{$this->id}_per_page" );
990 $per_page = (int) get_user_option( $option );
991 if ( empty( $per_page ) || $per_page < 1 ) {
992 $per_page = $this->get_option( 'per_page', 'default' );
997 if ( 'edit_comments_per_page' == $option ) {
998 $comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all';
999 $per_page = apply_filters( 'comments_per_page', $per_page, $comment_status );
1000 } elseif ( 'categories_per_page' == $option ) {
1001 $per_page = apply_filters( 'edit_categories_per_page', $per_page );
1003 $per_page = apply_filters( $option, $per_page );
1007 if ( isset( $this->post_type ) )
1008 $per_page = apply_filters( 'edit_posts_per_page', $per_page, $this->post_type );
1011 <div class="screen-options">
1012 <?php if ( $per_page_label ) : ?>
1013 <input type="number" step="1" min="1" max="999" class="screen-per-page" name="wp_screen_options[value]"
1014 id="<?php echo esc_attr( $option ); ?>" maxlength="3"
1015 value="<?php echo esc_attr( $per_page ); ?>" />
1016 <label for="<?php echo esc_attr( $option ); ?>">
1017 <?php echo esc_html( $per_page_label ); ?>
1021 echo get_submit_button( __( 'Apply' ), 'button', 'screen-options-apply', false ); ?>
1022 <input type='hidden' name='wp_screen_options[option]' value='<?php echo esc_attr($option); ?>' />