add_submenu_page()

最后更新于:2021-11-25 19:42:05

add_submenu_page( string$parent_slug, string$page_title, string$menu_title, string$capability, string$menu_slug, callable$function=”, int$position=null)

Add a submenu page.

参数

$parent_slug

(string) (Required) The slug name for the parent menu (or the file name of a standard GeChiUI admin page).

$page_title

(string) (Required) The text to be displayed in the title tags of the page when the menu is selected.

$menu_title

(string) (Required) The text to be used for the menu.

$capability

(string) (Required) The capability required for this menu to be displayed to the user.

$menu_slug

(string) (Required) The slug name to refer to this menu by. Should be unique for this menu and only include lowercase alphanumeric, dashes, and underscores characters to be compatible with sanitize_key().

$function

(callable) (Optional) The function to be called to output the content for this page.

Default value: ”

$position

(int) (Optional) The position in the menu order this item should appear.

Default value: null

响应

(string|false) The resulting page’s hook_suffix, or false if the user does not have the capability required.

源文件

文件: gc-admin/includes/plugin.php

function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '', $position = null ) {
	global $submenu, $menu, $_gc_real_parent_file, $_gc_submenu_nopriv,
		$_registered_pages, $_parent_pages;

	$menu_slug   = plugin_basename( $menu_slug );
	$parent_slug = plugin_basename( $parent_slug );

	if ( isset( $_gc_real_parent_file[ $parent_slug ] ) ) {
		$parent_slug = $_gc_real_parent_file[ $parent_slug ];
	}

	if ( ! current_user_can( $capability ) ) {
		$_gc_submenu_nopriv[ $parent_slug ][ $menu_slug ] = true;
		return false;
	}

	/*
	 * If the parent doesn't already have a submenu, add a link to the parent
	 * as the first item in the submenu. If the submenu file is the same as the
	 * parent file someone is trying to link back to the parent manually. In
	 * this case, don't automatically add a link back to avoid duplication.
	 */
	if ( ! isset( $submenu[ $parent_slug ] ) && $menu_slug !== $parent_slug ) {
		foreach ( (array) $menu as $parent_menu ) {
			if ( $parent_menu[2] === $parent_slug && current_user_can( $parent_menu[1] ) ) {
				$submenu[ $parent_slug ][] = array_slice( $parent_menu, 0, 4 );
			}
		}
	}

	$new_sub_menu = array( $menu_title, $capability, $menu_slug, $page_title );
	if ( ! is_int( $position ) ) {
		if ( null !== $position ) {
			_doing_it_wrong(
				__FUNCTION__,
				sprintf(
					/* translators: %s: add_submenu_page() */
					__( 'The seventh parameter passed to %s should be an integer representing menu position.' ),
					'<code>add_submenu_page()</code>'
				),
				'5.3.0'
			);
		}

		$submenu[ $parent_slug ][] = $new_sub_menu;
	} else {
		// Append the submenu if the parent item is not present in the submenu,
		// or if position is equal or higher than the number of items in the array.
		if ( ! isset( $submenu[ $parent_slug ] ) || $position >= count( $submenu[ $parent_slug ] ) ) {
			$submenu[ $parent_slug ][] = $new_sub_menu;
		} else {
			// Test for a negative position.
			$position = max( $position, 0 );
			if ( 0 === $position ) {
				// For negative or `0` positions, prepend the submenu.
				array_unshift( $submenu[ $parent_slug ], $new_sub_menu );
			} else {
				// Grab all of the items before the insertion point.
				$before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true );
				// Grab all of the items after the insertion point.
				$after_items = array_slice( $submenu[ $parent_slug ], $position, null, true );
				// Add the new item.
				$before_items[] = $new_sub_menu;
				// Merge the items.
				$submenu[ $parent_slug ] = array_merge( $before_items, $after_items );
			}
		}
	}
	// Sort the parent array.
	ksort( $submenu[ $parent_slug ] );

	$hookname = get_plugin_page_hookname( $menu_slug, $parent_slug );
	if ( ! empty( $function ) && ! empty( $hookname ) ) {
		add_action( $hookname, $function );
	}

	$_registered_pages[ $hookname ] = true;

	/*
	 * Backward-compatibility for plugins using add_management_page().
	 * See gc-admin/admin.php for redirect from edit.php to tools.php.
	 */
	if ( 'tools.php' === $parent_slug ) {
		$_registered_pages[ get_plugin_page_hookname( $menu_slug, 'edit.php' ) ] = true;
	}

	// No parent as top level.
	$_parent_pages[ $menu_slug ] = $parent_slug;

	return $hookname;
}
/**
 * Adds a submenu page under a custom post type parent.
 */
function books_register_ref_page() {
    add_submenu_page(
        'edit.php?post_type=book',
        __( 'Books Shortcode Reference', 'textdomain' ),
        __( 'Shortcode Reference', 'textdomain' ),
        'manage_options',
        'books-shortcode-ref',
        'books_ref_page_callback'
    );
}

/**
 * Display callback for the submenu page.
 */
function books_ref_page_callback() { 
    ?>
    <div class="wrap">
        <h1><?php _e( 'Books Shortcode Reference', 'textdomain' ); ?></h1>
        <p><?php _e( 'Helpful stuff here', 'textdomain' ); ?></p>
    </div>
    <?php
}
/**
 * Sub menu class
 *
 * @author Mostafa <mostafa.soufi@hotmail.com>
 */
class Sub_menu {

	/**
	 * Autoload method
	 * @return void
	 */
	public function __construct() {
		add_action( 'admin_menu', array(&$this, 'register_sub_menu') );
	}

	/**
	 * Register submenu
	 * @return void
	 */
	public function register_sub_menu() {
		add_submenu_page( 
			'options-general.php', 'Submenu title', 'Submenu title', 'manage_options', 'submenu-page', array(&$this, 'submenu_page_callback')
		);
	}

	/**
	 * Render submenu
	 * @return void
	 */
	public function submenu_page_callback() {
		echo '<div class="wrap">';
		echo '<h2>Submenu title</h2>';
		echo '</div>';
	}

}

new Sub_menu();
add_action('admin_menu', 'gcdocs_register_my_custom_submenu_page');

function gcdocs_register_my_custom_submenu_page() {
	add_submenu_page(
		'tools.php',
		'My Custom Submenu Page',
		'My Custom Submenu Page',
		'manage_options',
		'my-custom-submenu-page',
		'gcdocs_my_custom_submenu_page_callback' );
}

function gcdocs_my_custom_submenu_page_callback() {
	echo '<div class="wrap"><div id="icon-tools" class="icon32"></div>';
		echo '<h2>My Custom Submenu Page</h2>';
	echo '</div>';
}
Class GCDocs_AdminPage
{
    private $_plugin_name;

    private $_version;

    public function __construct( $plugin_name, $version )
    {
        $this->_plugin_name = $plugin_name;
        $this->_version = $version;
    }

    public function gcdocs_create_menu_and_submenu_page()
    {
        add_menu_page(
            'PAGE TITLE', 'MENU TITLE', 'CAPABILITY', 'menu_slug',
            array( $this, 'gcdocs_method_name_in_the_class' ), 'icon_url', 'POSITION'
        );
        add_submenu_page(
            'parent_slug', 'PAGE TITLE', 'MENU TITLE', 'CAPABILITY', 'menu_slug', 
            array( $this, 'gcdocs_method_name_in_the_class' )
        );
    }
}
$admin_page = new GCDocs_AdminPage;
add_action( 'admin_menu', array( $admin_page, 'gcdocs_create_menu_and_submenu_page' ) );