Make WordPress Core

Opened 12 years ago

Closed 11 years ago

#21454 closed enhancement (duplicate)

Admin menu top label linked to sub pages screen id?

Reported by: coenjacobs's profile CoenJacobs Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Administration Keywords: has-patch
Focuses: Cc:


Practical case: Within WooCommerce we allow the user to translate the main admin menu label, which says 'WooCommerce' by default. When this label is translated, it also changes the $screen->id property of the child pages. What used to be 'woocommerce_page_woocommerce_reports' is now 'boutique_page_woocommerce_status' (after translating to French) for example.

We want to continue offering to translate this and have a workaround to detect the correct subpage (for including stylesheets, javascripts on specific pages only), by using PHP's strstr. But I feel the ultimate goal, which allows a more 'clean' way is to be able to lock the id of the top level menu. This allows cleaner detection of screens based on screen->ids (which shouldn't change because of localisation) .

Attachments (1)

fix-constant-screen-ids.21454.diff (600 bytes) - added by mikejolley 12 years ago.
Uses the menu_slug for the screen ID instead of a sanitized page title.

Download all attachments as: .zip

Change History (7)

#1 follow-up: @scribu
12 years ago

  • Keywords reporter-feedback added
function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null ) {

Notice the $menu_slug parameter. You're probably generating it automatically from $page_title.

Similarly for add_submenu_page():

function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) {

#2 in reply to: ↑ 1 @CoenJacobs
12 years ago

Replying to scribu:

Notice the $menu_slug parameter. You're probably generating it automatically from $page_title.

No, I'm not referring to the $menu_slug parameter, but to the id of the sublevel page. This id seems to be generated from the top level menu title and thus will it change when you translate that. Example:

add_action( 'admin_menu', 'example_menu' );

function example_menu() {
	add_menu_page( 'Toplevel title', 'Toplevel nav', 'manage_options', 'toplevel_slug', 'example_toplevel_page' );
	add_submenu_page( 'toplevel_slug', 'Sublevel title', 'Sublevel nav', 'manage_options', 'sublevel_slug', 'example_sublevel_page' );

function example_toplevel_page() {
	$screen = get_current_screen();
	echo $screen->id; // toplevel_page_toplevel_slug

function example_sublevel_page() {
	$screen = get_current_screen();
	echo $screen->id; // toplevel-nav_page_sublevel_slug

When make the menu_title argument in the add_menu_page function call translatable, I enable the translator to change the $screen->id variable on all submenu pages, as the $menu title argument of the toplevel menu item gets prefixed to that id.

Last edited 12 years ago by CoenJacobs (previous) (diff)

#3 @scribu
12 years ago

  • Keywords needs-patch added; reporter-feedback removed

Confirmed. And I think we can fix it, since plugins don't construct the id themselves, but use what add_submenu_page() gives them.

Last edited 12 years ago by scribu (previous) (diff)

#4 @Mamaduka
12 years ago

  • Cc georgemamadashvili@… added

12 years ago

Uses the menu_slug for the screen ID instead of a sanitized page title.

#5 @mikejolley
12 years ago

  • Cc mikejolley added
  • Keywords has-patch added; needs-patch removed

Attached fix-constant-screen-ids.21454.diff which uses the menu_slug for the screen ID instead of a sanitized page title. $admin_page_hooks's key basically, instead of its value (which might not actually be used anywhere but I've left it in).

This fixes the case Coen posted making the subpage screen id change from "toplevel-nav_page_sublevel_slug" "toplevel_slug_page_sublevel_slug". This screen id will be constant and solve our problems :)

#6 @SergeyBiryukov
11 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #18857.

Note: See TracTickets for help on using tickets.