plugins_url()
Retrieves a URL within the plugins or mu-plugins directory.
Defaults to the plugins directory URL if no arguments are supplied.
Hooks from the function
Return
String
. Plugins URL link with optional paths appended.
Usage
plugins_url( $path, $plugin );
- $path(string)
- Extra path appended to the end of the URL, including the relative directory if $plugin is supplied.
Default: '' - $plugin(string)
- A full path to a file inside a plugin or mu-plugin. The URL will be relative to its directory. Typically this is done by passing __FILE__ as the argument.
Default: ''
Examples
#1 Another example with __FILE__
In the folder of the plugin is the directory images and the file wordpress.png, and in the same folder is an executable php file, in which we need to get a link to the picture, then in it use the following code:
echo '<img src="'. plugins_url( 'images/wordpress.png' , __FILE__ ) .'" > '; // get: <img src="http://www.example.com/wp-content/plugins/my-plugin/images/wordpress.png">.
#2 Demo:
echo plugins_url(); // returns: http://example.com/wp-content/plugins echo plugins_url( 'plugin-name/style.css' ); // returns: http://example.com/wp-content/plugins/plugin-name/style.css
#3 Dynamic path to the plugin file
We can specify the path in the first parameter hard: plugin-name/style.css
, but in this case, if we rename the plugin folder, the link will be broken, To avoid this, you can use __FILE__
in the second parameter, then the function itself will add the missing path between the plugins folder and the specified file.
Suppose our plugin folder is called plugin-name
and the file style.css
is in it, we need to get a link to this file, write it like this:
$plugins_url = plugins_url( 'style.css', __FILE__ ); echo $plugins_url; // returns: http://example.com/wp-content/plugins/plugin-name/style.css
In this case, the function must be called from the same directory where the file is.
#4 Calling from a sub-directory of the plugin directory
If plugins_url() is called from a file that is in the plugin sub-directory, then you must use dirname( __FILE__ ) in the second parameter:
echo '<img src="' . plugins_url( 'images/wordpress.png' , dirname( __FILE__ ) ) . '" > ';
Here in the second parameter we specify the path to the parent directory of the file from which the function is called.
Changelog
Since 2.6.0 | Introduced. |