Get the current screen in WordPress

current_screen action

/**
 * Adds notice to WooCommerce admin "Orders" list.
 *
 * @see https://wpcodebook.com/wordpress-current-screen/
 * @see https://developer.wordpress.org/reference/hooks/current_screen/
 */
add_action( 'current_screen', function ( $current_screen ) {
	if ( $current_screen && 'edit-shop_order' === $current_screen->id ) {
		add_action( 'admin_notices', function () {
			echo '<div class="notice notice-success is-dismissible">' .
				'<p>' . date_i18n( 'Y-m-d H:i:s' ) . '</p>' .
			'</div>';
		} );
	}
} );

get_current_screen() function

/**
 * Adds notice to WooCommerce admin "Products" list.
 *
 * @see https://wpcodebook.com/wordpress-current-screen/
 * @see https://developer.wordpress.org/reference/functions/get_current_screen/
 */
add_action( 'admin_notices', function () {
	$current_screen = get_current_screen();
	if ( $current_screen && 'edit-product' === $current_screen->id ) {
		echo '<div class="notice notice-success is-dismissible">' .
			'<p>' . date_i18n( 'Y-m-d H:i:s' ) . '</p>' .
		'</div>';
	}
} );

Leave a Comment