I have a plugin that is conflicting with some default wordpress functions. In the post and page creating/edit areas. I only need my functions on my settings pages. I have the js file loading in admin, no problem.
Can I tell the script not to load unless I’m viewing the settings pages for my plug?
You need to use a plugin page-specific script enqueue hook.
Edit
Best-practice method is to use
admin_enqueue_scripts-{hook}
, rather thanadmin_print_scirpts-{hook}
. But, because you’re targeting your own Plugin’s admin page specifically, either one is perfectly fine.The hook to avoid is the “global”
admin_print_scripts
.Original
The call would look like this:
And you define the
$page
hook like so:Answer copied directly out of the Codex:
To make your life easier and coding faster (no core file search needed), we’ve written the “Current Admin Info” Plugin.
This way you can easily see what you get back from the admin globals or the
get_current_screen()
function, which you then can access using the properties you see in the additional contextual help tabs.I don’t yet understand why, but according to Codex, you should only use
admin_enqueue_scripts
to enqueue styles/scripts in the admin. I have an open Question asking why, but haven’t yet received a satisfactory answer. The Codex entry might be in reference to this core dev’s post.Here’s how to do it the “correct” way (note: the
admin_enqueue_scripts
hook should only be called from another hooked function, e.g., as I’ve done in the second code block withadmin_menu
. If you try to hookadmin_enqueue_scripts
too early, you’ll get errors):Below is a fully functional plugin to play around with; it does nothing except inject your javascript file into the <head> of only your options page –