如何在不将其添加到菜单中的情况下添加 WordPress 管理页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3902760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:39:39  来源:igfitidea点击:

How do you add a WordPress admin page without adding it to the menu?

wordpress

提问by Nicky Hajal

I'm building a WordPress plugin and I'd like to have an edit-item page that can't be reached via the submenu (because then the item wouldn't be specified).

我正在构建一个 WordPress 插件,我想要一个无法通过子菜单访问的编辑项目页面(因为这样就不会指定项目)。

This resource (http://codex.wordpress.org/Adding_Administration_Menus) shows how to associate an admin page with a function, but not how to do so without adding it as a menu item.

此资源 ( http://codex.wordpress.org/Adding_Administration_Menus) 展示了如何将管理页面与功能相关联,但不展示如何在不将其添加为菜单项的情况下进行关联。

Can this be done?

这能做到吗?

Thanks!

谢谢!

采纳答案by John P Bloch

Yes, this can be done (well, technically, it would be more like registering the whole thing and then removing the menu item later), but It would just be easiest (I think) to check for parameters in the $_GETsuper-global to indicate that the user wishes to edit a specific item.

是的,这是可以做到的(好吧,从技术上讲,这更像是注册整个事情,然后稍后删除菜单项),但是最简单的(我认为)检查$_GET超级全局中的参数以指示用户希望编辑特定项目。

For example, you could have a page that lists items to edit, and clicking 'edit' only adds the item's ID to the current URL(query-string).

例如,您可以有一个页面列出要编辑的项目,单击“编辑”只会将项目的 ID 添加到当前 URL(查询字符串)。

In the function that displays this page, if ID is defined, give them the page to edit that item.

在显示此页面的函数中,如果定义了 ID,则为他们提供编辑该项目的页面。

Otherwise, give them the list view. That's how posts, pages, and other custom post types do it.

否则,给他们列表视图。这就是帖子、页面和其他自定义帖子类型的工作方式。

回答by Christof Coetzee

Best solution here http://wordpress.org/support/topic/add-backend-page-without-menu-item

这里的最佳解决方案http://wordpress.org/support/topic/add-backend-page-without-menu-item

use add_submenu_pagewith parent slug = null

add_submenu_page与父 slug 一起使用= null

回答by J.D.

I have finally discovered a way to do this that isn't an ugly hack, doesn't require JS to highlight the desired menu item (and submenu item), and works for regular menus registered by plugins (@Josh's answeronly works for custom post types).

我终于找到了一种方法,它不是一个丑陋的黑客,不需要 JS 突出显示所需的菜单项(和子菜单项),并且适用于插件注册的常规菜单(@Josh 的答案仅适用于自定义帖子类型)。

Essentially, you just need to register your submenu normally, but then hook into the 'submenu_file'filter to deregister it and optionally also set another submenu item to highlight instead.

本质上,您只需要正常注册您的子菜单,然后连接到'submenu_file'过滤器以取消注册,并可选择将另一个子菜单项设置为突出显示。

function so3902760_wp_admin_menu() {

    // Register the parent menu.
    add_menu_page(
        __( 'Parent title', 'textdomain' )
        , __( 'Parent', 'textdomain' )
        , 'manage_options'
        , 'my_parent_slug'
        , 'display_my_menu'
    );

    // Register the hidden submenu.
    add_submenu_page(
        'my_parent_slug' // Use the parent slug as usual.
        , __( 'Page title', 'textdomain' )
        , ''
        , 'manage_options'
        , 'my_hidden_submenu'
        , 'display_my_submenu'
    );
}
add_action( 'admin_menu', 'so3902760_wp_admin_menu' );

function so3902760_wp_admin_submenu_filter( $submenu_file ) {

    global $plugin_page;

    $hidden_submenus = array(
        'my_hidden_submenu' => true,
    );

    // Select another submenu item to highlight (optional).
    if ( $plugin_page && isset( $hidden_submenus[ $plugin_page ] ) ) {
        $submenu_file = 'submenu_to_highlight';
    }

    // Hide the submenu.
    foreach ( $hidden_submenus as $submenu => $unused ) {
        remove_submenu_page( 'my_parent_slug', $submenu );
    }

    return $submenu_file;
}
add_filter( 'submenu_file', 'so3902760_wp_admin_submenu_filter' );

回答by Developer-Sid

add_submenu_page with parent slug = null

add_submenu_page with parent slug = null

OR

或者

add_submenu_page with menu title = null

add_submenu_page 菜单标题 = null

回答by J.D.

Note: This solution doesn't automatically set the current menu and submenu item. If you want to highlight a particular menu as current when the hidden page is viewed, see my other answer.

注意:此解决方案不会自动设置当前菜单和子菜单项。如果您想在查看隐藏页面时将特定菜单突出显示为当前菜单,请参阅我的其他答案

From the answers that come before me, you can see that there are many ways to do this. However, there is another way that I think may be the best.

从我之前的答案中,您可以看到有很多方法可以做到这一点。但是,我认为还有另一种方法可能是最好的。

Loading the page differently based on the value of a $_GETquery var is one option, but it may not be what some people are looking for.

根据$_GET查询变量的值以不同的方式加载页面是一种选择,但这可能不是某些人想要的。

The suggestions regarding add_submenu_page()are on the right track, but each of the previous suggestions have problems. Setting $menu_titleto nulldoesn't keep the menu item from being displayed, it just makes it so the link doesn't have any text. The link still takes up some room in the menu though, so it looks funny. Setting the $parent_slugto nulldoesn't have this problem, but I noticed that the page's HTML titledoesn't display the $page_titletext.

有关的建议走add_submenu_page()在正确的轨道上,但之前的每条建议都有问题。设置$menu_titlenull不会阻止显示菜单项,它只会使链接没有任何文本。尽管如此,该链接仍然在菜单中占据了一些空间,所以看起来很有趣。设置$parent_slugnull没有这个问题,但是我注意到页面的HTMLtitle不显示$page_title文本。

My solution was to set $parent_slugto a fake menu slug, like 'i_dont_exist'. The menu item won't be displayed, and when viewing the admin screen the page title will be filled out properly.

我的解决方案是设置$parent_slug一个假的菜单 slug,比如'i_dont_exist'. 不会显示菜单项,并且在查看管理屏幕时,页面标题将正确填写。

add_submenu_page(
    '_doesnt_exist'
    ,__( 'Page title', 'textdomain' )
    ,''
    ,'manage_options'
    ,'menu_slug'
    ,'display_my_menu'
);

回答by Boopathi Rajan

use this code for creating new page without adding in menu

使用此代码创建新页面而不添加菜单

add_action( 'admin_menu', 'register_newpage' );

function register_newpage(){
    add_menu_page($appname, $appname, 'administrator','custompage', 'custom');
    remove_menu_page('custom');
}

function custom()
{
echo "hai";
}

回答by Jaime Gris

Yes. It is very possible to make a page cannot be reach via submenu, or even the main menu in the WP admin panel. See the code snippet below.

是的。很可能通过子菜单甚至 WP 管理面板中的主菜单无法访问页面。请参阅下面的代码片段。

function myplugin_render_edit_page() {
    // Code contains the UI for edit page.
}

/**
 * Manage menu items and pages.
 */
function myplugin_register_admin_page() {
    global $_registered_pages;

    $menu_slug = plugin_basename('myplugin.php');
    $hookname = get_plugin_page_hookname($menu_slug,'');
    if (!empty($hookname)) {
        add_action($hookname, 'myplugin_render_edit_page');
    }
    $_registered_pages[$hookname] = true;
}
add_action('admin_menu', 'myplugin_register_admin_page');

Hopefully, this will help.

希望这会有所帮助。

回答by Dragi Postolovski

Create sub menu page and parent slug leave it empty like this:

创建子菜单页面和父 slug 将其留空,如下所示:

// Create page were you can add new users.
    public function add_create_user_menu() {
        add_submenu_page(
            '',
            'Create User',
            'Create User',
            'manage_options',
            'create-user',
            array( $this, 'add_create_user_page' )
        );
    }

You can access it like this:

您可以像这样访问它:

<a href="/wp-admin/admin.php?page=create-user">Add New</a>

回答by ArtissTheGeek

I've tried all of the suggestions here but with various issues associated with each.

我已经尝试了这里的所有建议,但每个建议都存在各种问题。

The WordPress codex for add_submenu_pagenow gives the correct answer, which is to use options.php as your parent slug. I tried the trick of using a made up name but that gives permissions errors, equally use of null at various locations either causes the menu text to simply be missing (but still clickable) or for the browser title to go missing.

add_submenu_page 的 WordPress 代码现在给出了正确答案,即使用 options.php 作为您的父 slug。我尝试了使用虚构名称的技巧,但这会导致权限错误,在不同位置同样使用 null 会导致菜单文本丢失(但仍可点击)或浏览器标题丢失。

Using options.php worked and I've not seen any issues as a result of its use.

使用 options.php 有效,我没有看到任何使用它的结果。

回答by Josh

Using add_submenu_page with a parent of NULL definitely works, however if you want to keep the non-linked page associated with a particular menu (say a custom post type menu), you have to use a variation of @Boopathi's answer:

将 add_submenu_page 与 NULL 的父级一起使用肯定有效,但是,如果您想保留与特定菜单关联的非链接页面(例如自定义帖子类型菜单),则必须使用@Boopathi 答案的变体:

function my_hidden_submenu_page(){
    //add the submenu page the usual way
    add_submenu_page('edit.php?post_type=custom-type', 'My Page Title', 'My Page Title', 'manage_options', 'my-page-slug', 'my_page_callback');
    //then remove it
    remove_submenu_page('edit.php?post_type=custom-type','my-page-slug');
}
add_action('admin_menu', 'my_hidden_submenu_page');

It looks as though the two actions would cancel each other out, however remove_submenu_pagedoes not unregister the callback function; it merely removes the link.

看起来这两个动作会相互抵消,但是remove_submenu_page不会取消注册回调函数;它只是删除链接。

This way when someone is viewing your non-linked page, the correct navigation menu (our custom post type menu in this example) will still show as active.

这样,当有人查看您的非链接页面时,正确的导航菜单(本例中为我们的自定义帖子类型菜单)仍将显示为活动状态。