WP_Screen::get( string|WP_Screen $hook_name =  ): WP_Screen

In this article

Fetches a screen object.

Parameters

$hook_namestring|WP_Screenoptional
The hook name (also known as the hook suffix) used to determine the screen.
Defaults to the current $hook_suffix global.

Default:''

Return

WP_Screen Screen object.

Source

public static function get( $hook_name = '' ) {
	if ( $hook_name instanceof WP_Screen ) {
		return $hook_name;
	}

	$id              = '';
	$post_type       = null;
	$taxonomy        = null;
	$in_admin        = false;
	$action          = '';
	$is_block_editor = false;

	if ( $hook_name ) {
		$id = $hook_name;
	} elseif ( ! empty( $GLOBALS['hook_suffix'] ) ) {
		$id = $GLOBALS['hook_suffix'];
	}

	// For those pesky meta boxes.
	if ( $hook_name && post_type_exists( $hook_name ) ) {
		$post_type = $id;
		$id        = 'post'; // Changes later. Ends up being $base.
	} else {
		if ( str_ends_with( $id, '.php' ) ) {
			$id = substr( $id, 0, -4 );
		}

		if ( in_array( $id, array( 'post-new', 'link-add', 'media-new', 'user-new' ), true ) ) {
			$id     = substr( $id, 0, -4 );
			$action = 'add';
		}
	}

	if ( ! $post_type && $hook_name ) {
		if ( str_ends_with( $id, '-network' ) ) {
			$id       = substr( $id, 0, -8 );
			$in_admin = 'network';
		} elseif ( str_ends_with( $id, '-user' ) ) {
			$id       = substr( $id, 0, -5 );
			$in_admin = 'user';
		}

		$id = sanitize_key( $id );
		if ( 'edit-comments' !== $id && 'edit-tags' !== $id && str_starts_with( $id, 'edit-' ) ) {
			$maybe = substr( $id, 5 );
			if ( taxonomy_exists( $maybe ) ) {
				$id       = 'edit-tags';
				$taxonomy = $maybe;
			} elseif ( post_type_exists( $maybe ) ) {
				$id        = 'edit';
				$post_type = $maybe;
			}
		}

		if ( ! $in_admin ) {
			$in_admin = 'site';
		}
	} else {
		if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) {
			$in_admin = 'network';
		} elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN ) {
			$in_admin = 'user';
		} else {
			$in_admin = 'site';
		}
	}

	if ( 'index' === $id ) {
		$id = 'dashboard';
	} elseif ( 'front' === $id ) {
		$in_admin = false;
	}

	$base = $id;

	// If this is the current screen, see if we can be more accurate for post types and taxonomies.
	if ( ! $hook_name ) {
		if ( isset( $_REQUEST['post_type'] ) ) {
			$post_type = post_type_exists( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : false;
		}
		if ( isset( $_REQUEST['taxonomy'] ) ) {
			$taxonomy = taxonomy_exists( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : false;
		}

		switch ( $base ) {
			case 'post':
				if ( isset( $_GET['post'] ) && isset( $_POST['post_ID'] ) && (int) $_GET['post'] !== (int) $_POST['post_ID'] ) {
					wp_die( __( 'A post ID mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 );
				} elseif ( isset( $_GET['post'] ) ) {
					$post_id = (int) $_GET['post'];
				} elseif ( isset( $_POST['post_ID'] ) ) {
					$post_id = (int) $_POST['post_ID'];
				} else {
					$post_id = 0;
				}

				if ( $post_id ) {
					$post = get_post( $post_id );
					if ( $post ) {
						$post_type = $post->post_type;

						/** This filter is documented in wp-admin/post.php */
						$replace_editor = apply_filters( 'replace_editor', false, $post );

						if ( ! $replace_editor ) {
							$is_block_editor = use_block_editor_for_post( $post );
						}
					}
				}
				break;
			case 'edit-tags':
			case 'term':
				if ( null === $post_type && is_object_in_taxonomy( 'post', $taxonomy ? $taxonomy : 'post_tag' ) ) {
					$post_type = 'post';
				}
				break;
			case 'upload':
				$post_type = 'attachment';
				break;
		}
	}

	switch ( $base ) {
		case 'post':
			if ( null === $post_type ) {
				$post_type = 'post';
			}

			// When creating a new post, use the default block editor support value for the post type.
			if ( empty( $post_id ) ) {
				$is_block_editor = use_block_editor_for_post_type( $post_type );
			}

			$id = $post_type;
			break;
		case 'edit':
			if ( null === $post_type ) {
				$post_type = 'post';
			}
			$id .= '-' . $post_type;
			break;
		case 'edit-tags':
		case 'term':
			if ( null === $taxonomy ) {
				$taxonomy = 'post_tag';
			}
			// The edit-tags ID does not contain the post type. Look for it in the request.
			if ( null === $post_type ) {
				$post_type = 'post';
				if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) {
					$post_type = $_REQUEST['post_type'];
				}
			}

			$id = 'edit-' . $taxonomy;
			break;
	}

	if ( 'network' === $in_admin ) {
		$id   .= '-network';
		$base .= '-network';
	} elseif ( 'user' === $in_admin ) {
		$id   .= '-user';
		$base .= '-user';
	}

	if ( isset( self::$_registry[ $id ] ) ) {
		$screen = self::$_registry[ $id ];
		if ( get_current_screen() === $screen ) {
			return $screen;
		}
	} else {
		$screen     = new self();
		$screen->id = $id;
	}

	$screen->base            = $base;
	$screen->action          = $action;
	$screen->post_type       = (string) $post_type;
	$screen->taxonomy        = (string) $taxonomy;
	$screen->is_user         = ( 'user' === $in_admin );
	$screen->is_network      = ( 'network' === $in_admin );
	$screen->in_admin        = $in_admin;
	$screen->is_block_editor = $is_block_editor;

	self::$_registry[ $id ] = $screen;

	return $screen;
}

Hooks

apply_filters( ‘replace_editor’, bool $replace, WP_Post $post )

Allows replacement of the editor.

Changelog

VersionDescription
3.3.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.