How to get a slug name for a page without creating an admin page menu

ⅰ亾dé卋堺 提交于 2019-12-12 15:32:28

问题


I now use the add_submenu_page() function, but I don't want the edit page to appear in the admin menu.

I want to access the edit page from a list (another page) directly. But I need the slug as a hook_suffix.

I have in my-edit.php

/* Set up the administration functionality. */
add_action( 'admin_menu', 'my_edit_setup' );

function my_edit_setup() {
...
/* Add Edit Actionlist page. */
$myplugin->my_edit = add_submenu_page( 'myplugin', esc_attr__( 'Edit', 'myplugin' ), esc_attr__( 'Edit', 'myplugin' ), 7, 'my-edit', 'my_edit' );
...

In admin.php I have:

function my_admin_enqueue_style( $hook_suffix ) {

  $pages = array(
    'admin_page_projects',
        '...my-edit'
  );

  if ( in_array( $hook_suffix, $pages ) ) {
    wp_enqueue_style( 'myplugin-admin', trailingslashit( MYPLUGIN_URI ) . 'css/admin.css', false, '20110525', 'screen' );

You see I need the $hook_suffix, but I can't find out how to get this, without creating the admin menu item.


回答1:


Example of how to create an invisible sub menu (it gets attached to the Dashboard, index.php) and the correspondent $hook_suffix.

The page can be accessed through http://example.com/wp-admin/index.php?page=sample_page_hidden.

add_action(  'admin_menu', 'admin_menu_so_11593510' );
add_action( 'admin_enqueue_scripts', 'admin_enqueue_scripts_so_11593510' );

function admin_menu_so_11593510() 
{
    add_submenu_page(
        null, // doesn't shows up in the menu, submenu is attached to "index.php"
        'Test', 
        'Test', 
        'edit_pages', 
        'sample_page_hidden', 
        'menu_options_so_11593510'
    );
}

function menu_options_so_11593510() { echo 'Hello!'; }

function admin_enqueue_scripts_so_11593510( $hook_suffix )
{
    if ( 'dashboard_page_sample_page_hidden' == $hook_suffix ) {
        wp_enqueue_script( 'swfobject' );
    }
}


来源:https://stackoverflow.com/questions/11593510/how-to-get-a-slug-name-for-a-page-without-creating-an-admin-page-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!