如何手动设置自定义管理子菜单?

时间:2014-05-05 09:19:34

标签: wordpress

我正在开发我的第一个Wordpress插件。 可以说它有以下管理页面:

  • 常规设置
  • 添加新供应商
  • 供应商
  • 修改供应商

我不希望在菜单中显示编辑供应商,并希望使用供应商页面中的链接使其可访问。我的菜单和供应商页面是: enter image description here

生成菜单的代码:

function nes_general_settings_view () { 
    require_once("views/admin/general_settings.php");
}

function nes_vendor_view () { 
    require_once("views/admin/vendor.php");
}

function nes_vendor_new_view () {
    require_once("views/admin/vendor_new.php");
}

function nes_vendor_edit_view () {

    require_once("views/admin/vendor_edit.php");
}

add_action("admin_menu", function () {
    add_menu_page(
        "Service",  
        "Service",  
        "manage_options",            
        "nes_general_settings",      
        "nes_general_settings_view", 
        null,                        
        4                            
    );

    add_submenu_page( "nes_general_settings", "General Settings", "General Settings", 0, "nes_general_settings", "nes_general_settings_view");      
    add_submenu_page( "nes_general_settings", "Vendors", "Vendors", 0, "nes_vendor", "nes_vendor_view");
    add_submenu_page( "nes_general_settings", "New Vendor", "New Vendor", 0, "nes_vendor_new", "nes_vendor_new_view");
    add_submenu_page( "nes_fake_id", "Edit Vendor", "Edit Vendor", 0, "nes_vendor_edit", "nes_vendor_edit_view");       
});

和代码生成编辑供应商页面的链接:

<a href="<?=admin_url("admin.php?page=nes_vendor_edit")?>">Edit</a>

但是当我在编辑供应商页面时,我的菜单未被选中。 enter image description here

如何设置 服务&gt;当我在编辑供应商时,选择了供应商子菜单?

2 个答案:

答案 0 :(得分:2)

如果您的子菜单是自定义帖子类型,您可以通过设置&#34; show_in_menu&#34;的自定义帖子类型参数,轻松地将它们定义为自定义菜单的子菜单项。作为创建自定义后期类型时定义的slug。这样做将保留默认菜单打开/关闭并突出显示管理菜单中的当前子菜单项。

例如:

((cur + key.charAt(j) - 2 * 'A') % 26 + 'A' - 6));

然后在您的自定义帖子类型参数中定义show_in_menu以匹配菜单slug。

/* ————————————————————————— */
/* Content Types Menu Item
/* ————————————————————————— */

add_menu_page(
    'Content Types',
    'Content Types',
    'manage_options',
    'custom-content', // menu slug
    'ds_create_new_submenu',
    'dashicons-editor-table',
    null
);

为简单起见缩短了代码示例

答案 1 :(得分:1)

这必须使用jQuery到add the proper classes到管理菜单。

使用以下命令打印脚本:

# Grab the slug to print only in this screen
$hook = add_submenu_page( 
    null, # better than fake id
    "Edit Vendor", 
    "Edit Vendor", 
    'manage_options', # IMPORTANT, don't use levels (0-9), they're deprecated
    "nes_vendor_edit", 
    function() {
        echo '<h1>nes_vendor_edit_view</h1>';
    }
);

add_action( "admin_footer-$hook", function()
{
    # http://stackoverflow.com/questions/5673269/what-is-the-advantage-of-using-heredoc-in-php
    echo <<<HTML
        <script type="text/javascript">
        jQuery(document).ready( function($) {
            $('#toplevel_page_nes_general_settings')
                .addClass('current wp-has-current-submenu wp-menu-open');
        });     
        </script>
HTML;
});