WordPress在admin子菜单下添加页面并在菜单中保留父子菜单页面的活动状态

时间:2020-12-03 作者:sariDon

我在“管理”菜单中添加了一个页面(“pxmag”菜单)和一个子菜单(“pxmag计划”)。在子菜单(pxmag计划)下设置了另一个页面(pxmag计划编辑)作为父页面。

public function __construct()
{
    require(\'pxMagAdminPlans.class.php\');
    $this->admPlanObj= new pxMagAdminPlans();

    add_action(\'admin_menu\', array($this, \'add_plan_admin_menu\'));
}

public function add_plan_admin_menu()
{
    add_menu_page(__(\'Dashboard\', \'textdomain\'), get_bloginfo(\'name\'), \'manage_options\', \'pxmag-menu\', array($this, \'pxmag_dash\'), \'dashicons-welcome-view-site\', 6);
        
    add_submenu_page(\'pxmag-menu\', __(\'Subscription Plans\', \'textdomain\'), \'Plans\', \'manage_options\', \'pxmag-plans\', array($this->admPlanObj, \'plan_admin_menu_page\'));
    add_submenu_page(\'pxmag-plans\', __(\'Add/edit Plans\', \'textdomain\'), \'Add/edit plans\', \'manage_options\', \'pxmag-plans-edit\', array($this->admPlanObj, \'plan_admin_menu_edit\'));
}
所有菜单和子菜单页面加载良好。

但是,当我打开此页面(pxmag计划编辑)时,Wordpress admin中的菜单选项没有显示任何当前项,而pxmag计划应该是当前选项。

(它的工作原理是:当我单击“帖子>类别”并随后打开“编辑类别”页面时,菜单中的“帖子>类别”选项保持选中状态)。

出了什么问题?正确的流程是什么?

2 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

如果我理解正确,您希望;“计划”;“打开”时要突出显示的子菜单;添加/编辑计划”;(pxmag-plans-edit) page,然后您可以这样做:

使用add_menu_classes hook 突出显示pxmag-menu 菜单:

function my_add_menu_classes( $menu ) {
    // Do nothing if not on the "Add/edit plans" page.
    global $plugin_page;
    if ( \'pxmag-plans-edit\' !== $plugin_page ) {
        return $menu;
    }

    foreach ( $menu as $i => $item ) {
        if ( \'pxmag-menu\' === $item[2] ) {
            $menu[ $i ][4] = add_cssclass( \'wp-has-current-submenu wp-menu-open\', $item[4] );
        }
    }

    return $menu;
}
add_filter( \'add_menu_classes\', \'my_add_menu_classes\' );
submenu_file hook 突出显示;“计划”;(pxmag-plans) 子菜单:

function my_submenu_file( $submenu_file, $parent_file ) {
    global $plugin_page;
    return ( \'pxmag-plans-edit\' === $plugin_page )
        ? \'pxmag-plans\' : $submenu_file;
}
add_filter( \'submenu_file\', \'my_submenu_file\', 10, 2 );

SO网友:sariDon

我稍微修改了发布为答案的代码@Sally CJ 要处理多个案例,请执行以下操作:

public function __construct()
{
    require(\'pxMagAdminPlans.class.php\');
    require(\'pxMagAdminPromo.class.php\');
        
    $this->admPlanObj= new pxMagAdminPlans();
    $this->admPromoObj= new pxMagAdminPromo();
        
    add_action(\'admin_menu\', array($this, \'add_plan_admin_menu\'));
        
    add_filter(\'add_menu_classes\', array($this, \'adjust_menu_classes\'));
    add_filter(\'submenu_file\', array($this, \'adjust_submenu_file\'), 10, 2 );
}

public function add_plan_admin_menu()
{
    add_menu_page(__(\'Dashboard\', \'textdomain\'), get_bloginfo(\'name\'), \'manage_options\', \'pxmag-menu\', array($this, \'pxmag_dash\'), \'dashicons-welcome-view-site\', 6);
        
    add_submenu_page(\'pxmag-menu\', __(\'Subscription Plans\', \'textdomain\'), \'Plans\', \'manage_options\', \'pxmag-plans\', array($this->admPlanObj, \'plan_admin_menu_page\'));
    add_submenu_page(\'pxmag-plans\', __(\'Add/edit Plans\', \'textdomain\'), \'Add/edit plans\', \'manage_options\', \'pxmag-plans-edit\', array($this->admPlanObj, \'plan_admin_menu_edit\'));
        
    add_submenu_page(\'pxmag-menu\', __(\'Manage Promotions\', \'textdomain\'), \'Promotions\', \'manage_options\', \'pxmag-promotions\', array($this->admPromoObj, \'get_promotions\'));
    add_submenu_page(\'pxmag-promotions\', __(\'Add/Edit\', \'textdomain\'), \'Add/Edit\', \'manage_options\', \'pxmag-promotions-edit\', array($this->admPromoObj, \'manage_promotions\'));
        
    remove_submenu_page(\'pxmag-menu\',\'pxmag-menu\');
}
    
function adjust_submenu_file($submenu_file, $parent_file) {
    global $plugin_page;
    $retsub = $submenu_file;
    if(\'pxmag-plans-edit\' === $plugin_page)
        $retsub = \'pxmag-plans\';
    elseif (\'pxmag-promotions-edit\' === $plugin_page)
        $retsub = \'pxmag-promotions\';
    return $retsub;
}

function adjust_menu_classes($menu)
{
    global $plugin_page;
    $retmenu = $menu;

    if ((\'pxmag-promotions-edit\' == $plugin_page) || (\'pxmag-plans-edit\' == $plugin_page))
    {
        foreach ($menu as $i => $item)
        {
            if (\'pxmag-menu\' === $item[2])
            {
                    $retmenu[$i][4] = add_cssclass(\'wp-has-current-submenu wp-menu-open\', $item[4]);
            }
        }
    }
    return $retmenu;
}

相关推荐