Your custom admin page’s hook suffix might get translated

If you’re relying on the load-{hook} action or checking get_current_screen() to load assets on your custom admin page, your assets might not get loaded when your plugin is translated.

Why? Because there’s an old bug in WordPress which loads the page hook name from the page title instead of the slug. This looks like one of those things that’s just slipped under the radar for several releases. Hopefully it gets noticed again soon.

Here’s what happens. Let’s say you’ve created a top-level admin page with a sub-menu page underneath it.

Now if you want to enqueue an asset on that sub-page, there are two ways it’s typically done.

The problem arises when your plugin is translated. The hook suffix used in load-{page} and the $screen->base both use the translated version of the menu_title not the menu_slug! If the title was translated from MyPlugin-Parent to MyGermanPlugin-Parent, the hook would change from load-myplugin-parent_page_myplugin-subpage to load-mygermanplugin-parent_page_myplugin-subpage.

Crazy. How are you supposed to know the translated hook? apocalip, who initially reported the issue, also posted a workaround that involves going directly to the $admin_page_hooks global.

Here are the previous examples with a translation-safe implementation.

An important thing to note is that I only experienced this problem on a sub-page. When checking the $screen->base on the top-level page, I found the menu slug was still used in the base (toplevel_page_myplugin-parent) whether or not a translation was loaded.

Hopefully this bug, which has a simple patch ready to go, will get shipped soon.