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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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:
-
Use the
add_menu_classeshook to highlight thepxmag-menumenu: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' ); -
Use the
submenu_filehook 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 );
Method 2
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;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0