WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 2 years ago

#21454 closed enhancement (duplicate)

Admin menu top label linked to sub pages screen id?

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

Description

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 3 years ago.
Uses the menu_slug for the screen ID instead of a sanitized page title.

Download all attachments as: .zip

Change History (7)

comment:1 follow-up: @scribu3 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 = '' ) {

comment:2 in reply to: ↑ 1 @CoenJacobs3 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 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.

Version 0, edited 3 years ago by CoenJacobs (next)

comment:3 @scribu3 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 3 years ago by scribu (previous) (diff)

comment:4 @Mamaduka3 years ago

  • Cc georgemamadashvili@… added

@mikejolley3 years ago

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

comment:5 @mikejolley3 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 :)

comment:6 @SergeyBiryukov2 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.