plugins_url()WP 2.6.0

Retrieves a URL within the plugins or mu-plugins directory.

Defaults to the plugins directory URL if no arguments are supplied.

1 time — 0.000034 sec (very fast) | 50000 times — 0.71 sec (very fast)
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

0

#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">.
0

#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
0

#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.

0

#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.

plugins_url() code WP 6.5.2

function plugins_url( $path = '', $plugin = '' ) {

	$path          = wp_normalize_path( $path );
	$plugin        = wp_normalize_path( $plugin );
	$mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );

	if ( ! empty( $plugin ) && str_starts_with( $plugin, $mu_plugin_dir ) ) {
		$url = WPMU_PLUGIN_URL;
	} else {
		$url = WP_PLUGIN_URL;
	}

	$url = set_url_scheme( $url );

	if ( ! empty( $plugin ) && is_string( $plugin ) ) {
		$folder = dirname( plugin_basename( $plugin ) );
		if ( '.' !== $folder ) {
			$url .= '/' . ltrim( $folder, '/' );
		}
	}

	if ( $path && is_string( $path ) ) {
		$url .= '/' . ltrim( $path, '/' );
	}

	/**
	 * Filters the URL to the plugins directory.
	 *
	 * @since 2.8.0
	 *
	 * @param string $url    The complete URL to the plugins directory including scheme and path.
	 * @param string $path   Path relative to the URL to the plugins directory. Blank string
	 *                       if no path is specified.
	 * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
	 *                       is specified.
	 */
	return apply_filters( 'plugins_url', $url, $path, $plugin );
}