如何在WordPress自定义插件中启用其他页面? [英] How to enable additional page in WordPress custom plugin?

查看:143
本文介绍了如何在WordPress自定义插件中启用其他页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义WordPress插件,向我显示了数据库中的数据列表.我正在通过以下方式注册它的页面:

I have a custom WordPress plugin that is showing me a list of data from database. I am registering it's page via:

add_menu_page( 
    'Naročila', 
    'Vsa naročila', 
    'administrator', 
    'listaj-narocila', 
    array( &$this, 'listaj_narocila' )
);

然后我当然具有函数lista_narocila,该函数向我显示我的数据.
所以,目前我的网址是:

And then of course I have function lista_narocila which is showing me my data.
So, currently my URL is:

http://domain.com/wp-admin/admin.php?page=listaj-narocila

然后我将来自数据库的数据显示在表中.现在,我为每个记录设置了DELETE和EDIT按钮,但是我很难弄清楚如何在WordPress中注册自定义"url"或"custom page"以允许我拥有URL:

And I show my data from database in a table. Now I have button DELETE and EDIT for each record, but I have a hard time figuring it out, how to register custom "url" or "custom page" inside WordPress that would allow me to have URL:

http://domain.com/wp-admin/admin.php?page=single-narocilo?id=X

我知道我可以尝试使用add_menu_page,但我希望此页面位于管理菜单中.仅作为URL提供.目前,我没有访问错误.

I know I can try with add_menu_page but I don't want this page to be in admin menus. Just to be available as URL. Currently I get no access error.

推荐答案

您可以创建子菜单页面,并

You can create a sub menu page and pass null as its parent:

$ parent_slug
如果要创建一个不在任何菜单中显示的页面,请使用NULL或将其设置为"options.php".

$parent_slug
Use NULL or set to 'options.php' if you want to create a page that doesn't appear in any menu.

演示:

add_action('admin_menu', function() 
{
    # Main page
    add_menu_page( 
        'Vsa', 
        'Vsa', 
        'add_users', // Capability, not role
        'listaj-narocila', 
        function(){ 
            printf(
                '<h2>%s</h2><a href="%s">%s</a>',
                __( 'Main page' ),
                admin_url( 'admin.php?page=single-norcilo&id='.rand(1,25) ),
                __( 'Hidden sub page' )
            );
        },
        'http://sstatic.net/stackexchange/img/favicon.ico'
    );  

    # Child page    
    $hook = add_submenu_page(
        null,
        'Norcilo',
        'Norcilo',
        'add_users',
        'single-norcilo',
        function(){ 
            printf(
                '<h2>%s</h2><a href="%s">%s</a>',
                __( 'Hidden sub page' ),
                admin_url( 'admin.php?page=listaj-narocila' ),
                __( 'back' )
            );
        }
    );

    # Enqueue script in submenu page to fix the current menu indicator
    add_action( "admin_footer-$hook", function()
    {
        echo <<<HTML
<script type="text/javascript">
jQuery(document).ready( function($) {
    $('#toplevel_page_listaj-narocila')
        .removeClass('wp-not-current-submenu')
        .addClass('current');
});     
</script>
HTML;

    });
});

另一种方法: https://wordpress.stackexchange.com/a/114818/12615

这篇关于如何在WordPress自定义插件中启用其他页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆