I’ve created a menu page using the following function:
add_menu_page(
'Nonpareil options',
'Nonpareil options',
'administrator',
'nonpareil_theme_options',
'nonpareil_theme_display'
);
Now I want to load a js file only on this page. I’m trying to do so properly by enqueueing it only on my new page:
function nonpareil_options_js_enqueue($hook_suffix) {
if( 'nonpareil_theme_options' != $hook_suffix )
return;
wp_enqueue_script( 'nonpareil-options', get_template_directory_uri().'/js/nonpareil-options.js', array('jquery') );
}
add_action( 'admin_enqueue_scripts', 'nonpareil_options_js_enqueue' );
The script is not being enqueued. I think the issue is probably that $hook_suffix != “nonpareil_theme_options” on my new page, but in that case I have no idea what to put instead. What’s wrong here?
Thank you!
You could try replacing
with
if you have the custom menu added like this:
Edit:
It looks like you are using this
admin_enqueue_scripts
example in the Codex:http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts#Example:_Target_a_Specific_Admin_Page
so in your case the Codex example would be like this:
In the file
/wp-admin/admin-header.php
you have the followingso you can see the difference,
admin_print_scripts
doesn’t take input, butadmin_enqueue_scripts
does (and this is the filter you are using in your code example).If you wonder where
toplevel_page_
comes from, you can check out the source code forget_plugin_page_hookname()
since it is generating the value for$hook_suffix
in your case.Conclusion:
Add
toplevel_page_
in front of your menu slug.You can’t just import a particular variable into an action by naming it in the callback function. The variable imported, if available, is the variable supplied when the
do_action
function for that hook name runs. What you call that variable inside your callback function is irrelevant.admin_print_scripts
does not pass a variable at all. You can prove this to yourself with this disposable function (which will break stuff):Again, that will break the site. You will have to remove the function (or at least the
die
part of it) before the admin pages will work again. I work on a development server so that is not a problem for me.admin_enqueue-scripts
does pass a variable, and what you are trying to do should work but requires additional logic in the function whereas doing the following does not. The logic is handled by the hook itself.pagenow = 'some_string_resembling_the_page_slug'
admin_print_scripts-some_string_resembling_the_page_slug
In your case something like: