0

I have added a page in the admin menu(pxmag-menu) and a submenu(pxmag-plans). There is another page(pxmag-plans-edit) set under the submenu(pxmag-plans) as the parent page.

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'));
}

All the menu and submenu pages load fine.

But, when I open this page(pxmag-plans-edit), the menu selection in the Wordpress admin shows nothing as current item, whereas the pxmag-plans is supposed to be the current selection.

(It is supposed to work like: when I click 'Posts > Categories' and subsequently open the 'edit category' page, the 'Posts > Categories' option in menu keeps selected).

What is going wrong? What is the correct process?

1

If I understand it correctly — you want the "Plans" sub-menu to be highlighted when on the "Add/edit plans" (pxmag-plans-edit) page, then you can do it like so:

  1. Use the add_menu_classes hook to highlight the pxmag-menu 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' );
    
  2. Use the submenu_file hook to highlight the "Plans" (pxmag-plans) sub-menu:

    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 );
    
1
  • 1
    Great... This works. I have modified the code for multiple cases. Posting them soon. – sariDon Dec 4 '20 at 5:58
0

I have slightly modified the code posted as answer by @Sally CJ to work with multiple cases:

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.