plugins_url
will output the absolute file path of the plugins directory in your WordPress install.
This of course won’t work for a client (browser) calling a script.
To access scripts from the plugins directory, you’ll want to use plugin_dir_url()
which will get you the url of the plugin directory.
Some things to note about plugin_dir_url()
- You need to specify the directory of the plugin name your script is located
- The function output contains a trailing slash, so you won’t need to concatenate a slash.
Lets say your plugin is called “my_plugin” and the script is located in a “js” directory, your script registration would look something like this:
wp_register_script('r_footer', plugins_dir_url() . 'my_plugin/js/responsiveFooter.js', array('jquery'),'1.1', true);
Note the omission of __FILE__
which will output the absolute path of the current file (not what you want).
If your script is in your active theme, you’ll want to use a different function: get_stylesheet_directory_uri
Some things to note about get_stylesheet_directory_uri
- It requires a trailing slash unlike
plugin_dir_url()
- You will need to specify the directory path in the theme where your script is located.
- This function works especially well if you are working with child themes, but a child theme is not required. If you are working with a child theme, this function will get the path to the style.css file in your child theme rather than the parent theme.
- Note that the function is uri NOT url
So lets say your theme is called “my_theme” and the javascript is located in a “js” file, your registration script would look something like this:
wp_register_script('r_footer', get_stylesheet_directory_uri() . '/my_theme/js/responsiveFooter.js', array('jquery'),'1.1', true);
Links to documentation:
- plugin_dir_url – https://codex.wordpress.org/Function_Reference/plugin_dir_url
- get_stylesheet_uri – https://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri
- get_template_directory_uri (which is an alternative function to get_stylesheet_uri, but isn’t advised for child themes) – https://developer.wordpress.org/reference/functions/get_template_directory_uri/
- plugins_url – https://codex.wordpress.org/Function_Reference/plugins_url