Opened 12 years ago
Closed 12 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->id
s (which shouldn't change because of localisation) .
Attachments (1)
Change History (7)
#2
in reply to:
↑ 1
@
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.
#3
@
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.
#5
@
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 :)
Notice the $menu_slug parameter. You're probably generating it automatically from $page_title.
Similarly for add_submenu_page():