0

I am adding a submenu page as follows:

add_submenu_page( $slug, __('Categories','acs'), __('Categories','acs'), $cap, 'edit-tags.php?taxonomy=acs-field-group-category&post_type=acs-field-group' );

But the submenu does not display as active when selected.

So after wondering... I went into WP Core and added a var dump at wp-amdin/menu-header.php

Right where the check is made, to add the .current CSS classname.

var_dump($submenu_file);
var_dump($sub_item[2]);

And... the output is a mystery:

string(77) "edit-tags.php?taxonomy=acs-field-group-category&post_type=acs-field-group"
string(73) "edit-tags.php?taxonomy=acs-field-group-category&post_type=acs-field-group"

Same string, different length!

Of course it must be some encoding game, but I'm using phpStorm with encoding set to UTF-8.

I don't know how to get over this, help will be appreciated.

1 Answer 1

2
string(77) "edit-tags.php?taxonomy=acs-field-group-category&post_type=acs-field-group"
string(73) "edit-tags.php?taxonomy=acs-field-group-category&post_type=acs-field-group"

Same string, different length!

No, they're not the same string — if you had viewed the raw/server-generated HTML source, or used this instead of the var_dump($submenu_file);:

echo '<pre>'; var_dump($submenu_file, $sub_item[2]); echo '</pre>';

Then you would have seen the correct string which uses &amp; instead of just &:

string(77) "edit-tags.php?taxonomy=acs-field-group-category&amp;post_type=acs-field-group"

So that means, your (sub-)menu slug (the 5th parameter for add_submenu_page()) needs to use the ampersand's HTML entity, i.e. &amp;, like so:

add_submenu_page( $slug, __('Categories','acs'), __('Categories','acs'), $cap, // wrapped for brevity
    'edit-tags.php?taxonomy=acs-field-group-category&amp;post_type=acs-field-group' ); // ← use &amp;

That way, the submenu would be active when the submenu page is loaded.

And actually, you can see a core example here in wp-admin/edit-tags.php (WordPress v6.0.1), which implies that $submenu_file (the submenu slug) expects &amp; to be used.

PS: Remember to restore the original wp-admin/menu-header.php file — remove your "hack"/modifications.

3
  • I have to thank you for this. Always thought that var_dump() is the correct way to dump a var's info. Using the <pre> tag to avoid losing special information is something new I learn today. Commented Aug 1, 2022 at 7:04
  • 1
    You're welcome, and actually, you could just use the submenu_file filter (along with error_log()) to debug the submenu file that's used on the current page. You could also use it (and the parent_file filter) to change the submenu that would be highlighted on that page.
    – Sally CJ
    Commented Aug 1, 2022 at 7:47
  • 1
    A million thanks! :) Commented Aug 1, 2022 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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