I am trying to add in a script and css file for my plugin into the admin header.
Is there a function similar to
get_bloginfo('url') that I could use to reference the files correctly without having to hard code url's?
Then like any good developer you should be using
If the styles/scripts are only to be loaded in the plugin page(s), then you should ideally use some conditional logic inside a generic admin head hook, or alternatively hook some enqueues specifically to the page(s).
First work out the hook for your page or pages, this is the fourth parameter used in calls to
In this case, the two hooks(or handles) are
Now, to hook scripts or styles into the admin head specifically for plugin pages there's a few different ways we can go about it, but i'll cover the two most obvious(and my preferred) methods.
$hook represents the hook(or handle) for a given admin page, every admin page has one.
Enqueue JS files for both parent and submenu page
Enqueue stylesheet files for both parent and submenu page
admin_print_scripts + admin_print_styles
With this approach we'll use the more generic hooks that run for every admin page, but with a little conditional logic we can determine what kind of page we're viewing and return if it's not one of the plugin's pages(so it essentially has the same effect as the first approach).
Enqueue JS for any of the plugin's page
Enqueue CSS for any of the plugin's page
If you're intending to enqueue a script or style for more than one page, you can make that process alot easier by registering the script.
The advantage to registering a script or style is that calling that file then becomes a simple case of..
..same applies for the style equivalent.
This avoids any need to provide a path, dependancies and so forth with every enqueue call.
I had to wrap the answer up toward the end due to time, but hopefully i've provided enough valuable information to work with. If you get stuck trying to understand something, just post a comment.. ;)
you can use WP_PLUGIN_URL like so:
or you can define your own like this:
on the main plugin file.
hope this helps.
If you want the url to a file in your plugin directory, you should use
Remember, people can rename your plugin directory, so don't hard-code it in your file.
I typically use this:
Then to reference a file inside a folder called "resources" in your main plugin directory, you'd do: