get_admin_page_parent()WP 1.5.0

Gets the parent file of the current admin page.

1 time — 0.000001 sec (speed of light) | 50000 times — 0.60 sec (very fast) | PHP 7.3.20, WP 5.5.3

No Hooks.

Return

String. The parent file of the current admin page.

Usage

get_admin_page_parent( $parent_page );
$parent_page(string)
The slug name for the parent menu (or the file name of a standard WordPress admin page).
Default: empty string

Examples

0

#1 Display the name of the parent file of the default menu item

Add a sub-menu item with add_submenu_page() to the Tools menu item.

add_action( 'admin_menu', 'mat_add_submenu_page' );

function mat_add_submenu_page() {

	add_submenu_page(
		'tools.php',
		'Massive addition of terms',
		'Massive addition of terms',
		'manage_categories',
		'mat-admin',
		'mat_admin_page_content'
	);
}

function mat_admin_page_content() {
	echo get_admin_page_parent(); // output: tools.php
}
0

#2 Display the name of the parent file in CF7

Add a submenu to the Contact Form 7 main menu.

add_action( 'admin_menu', 'cf7_submodule_add_menu_page' );

function cf7_submodule_add_menu_page() {

	add_submenu_page(
		'wpcf7',
		'Module for CF7',
		'Module page for CF7',
		'wpcf7_read_contact_forms',
		'cf7-submodule-admin',
		'cf7_submodule_admin_page_content'
	);
}

function cf7_submodule_admin_page_content() {
	echo get_admin_page_parent(); // output: wpcf7
}

Notes

  • Global. String. $parent_file
  • Global. Array. $menu
  • Global. Array. $submenu
  • Global. String. $pagenow The filename of the current screen.
  • Global. String. $typenow The post type of the current screen.
  • Global. String. $plugin_page
  • Global. Array. $_wp_real_parent_file
  • Global. Array. $_wp_menu_nopriv
  • Global. Array. $_wp_submenu_nopriv

Changelog

Since 1.5.0 Introduced.

get_admin_page_parent() code WP 6.6

function get_admin_page_parent( $parent_page = '' ) {
	global $parent_file, $menu, $submenu, $pagenow, $typenow,
		$plugin_page, $_wp_real_parent_file, $_wp_menu_nopriv, $_wp_submenu_nopriv;

	if ( ! empty( $parent_page ) && 'admin.php' !== $parent_page ) {
		if ( isset( $_wp_real_parent_file[ $parent_page ] ) ) {
			$parent_page = $_wp_real_parent_file[ $parent_page ];
		}

		return $parent_page;
	}

	if ( 'admin.php' === $pagenow && isset( $plugin_page ) ) {
		foreach ( (array) $menu as $parent_menu ) {
			if ( $parent_menu[2] === $plugin_page ) {
				$parent_file = $plugin_page;

				if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) {
					$parent_file = $_wp_real_parent_file[ $parent_file ];
				}

				return $parent_file;
			}
		}
		if ( isset( $_wp_menu_nopriv[ $plugin_page ] ) ) {
			$parent_file = $plugin_page;

			if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) {
					$parent_file = $_wp_real_parent_file[ $parent_file ];
			}

			return $parent_file;
		}
	}

	if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[ $pagenow ][ $plugin_page ] ) ) {
		$parent_file = $pagenow;

		if ( isset( $_wp_real_parent_file[ $parent_file ] ) ) {
			$parent_file = $_wp_real_parent_file[ $parent_file ];
		}

		return $parent_file;
	}

	foreach ( array_keys( (array) $submenu ) as $parent_page ) {
		foreach ( $submenu[ $parent_page ] as $submenu_array ) {
			if ( isset( $_wp_real_parent_file[ $parent_page ] ) ) {
				$parent_page = $_wp_real_parent_file[ $parent_page ];
			}

			if ( ! empty( $typenow ) && "$pagenow?post_type=$typenow" === $submenu_array[2] ) {
				$parent_file = $parent_page;
				return $parent_page;
			} elseif ( empty( $typenow ) && $pagenow === $submenu_array[2]
				&& ( empty( $parent_file ) || ! str_contains( $parent_file, '?' ) )
			) {
				$parent_file = $parent_page;
				return $parent_page;
			} elseif ( isset( $plugin_page ) && $plugin_page === $submenu_array[2] ) {
				$parent_file = $parent_page;
				return $parent_page;
			}
		}
	}

	if ( empty( $parent_file ) ) {
		$parent_file = '';
	}
	return '';
}