plugins_url()WP 2.6.0

Gets the URL of the plugins or mu (must use) plugins folder (without a trailing slash).

The obtained URL can be supplemented by specifying the first parameter $path.

You can also use the magic PHP constant __FILE__ for the second parameter to get the path to the folder where the specified __FILE__ is located. The function will compare the path to the plugins folder and the full path to the file from which the function is called, and will supplement the missing path from the plugins directory to the specified file.

Uses constants: WP_PLUGIN_URL or WPMU_PLUGIN_URL as the base for creating the final URL.

Notes
  1. It is not recommended to use constants WP_PLUGIN_URL, WPMU_PLUGIN_URL, instead of this function.

  2. The function is not recommended to be called from the global context of the plugin. It is better to call it later, after all plugins have been loaded - this is the hook plugins_loaded and any later hooks. This is necessary to ensure that functions that can add other plugins to modify the result of this function are already hooked to plugins_url.

Use plugin_dir_url( __FILE__ ) in the main plugin file to get the URL to the main plugin folder.

Use WP_PLUGIN_DIR or WPMU_PLUGIN_DIR to get the path to the plugins folder, not the URL.

1 time — 0.000034 sec (very fast) | 50000 times — 0.71 sec (very fast)
Hooks from the function

Returns

String. URL to the specified plugin file.

Usage

plugins_url( $path, $plugin );
$path(string)
Path to the plugin file (relative to the plugins directory), the URL of which needs to be obtained,
Default: ''
$plugin(string)
Path after the plugins directory, which should be after the plugins directory and before the specified file in the first parameter. The constant __FILE__ is often used in this parameter, see examples.
Default: ''

Examples

1

#1 URLs from a category to a higher level

When plugins_url() is called from a file located in a subdirectory of the plugin, and you need to get the URL of a directory one level higher, use __DIR__ as the second parameter.

For example, if the code is called from the file .../my-plugin/includes/enqueue.php, but you need the URL to .../my-plugin/images/img.png:

$img_url = plugins_url( 'images/img.png' , __DIR__ );

// or
$parent_url = dirname( plugins_url( '', __FILE__ ) );
$img_url = "$parent_url/images/img.png";
0

#2 Demo

echo plugins_url();
// http://example.com/wp-content/plugins

echo plugins_url( 'plugin-name/style.css' );
// http://example.com/wp-content/plugins/plugin-name/style.css

// is called from a file in the root of the plugin
echo plugins_url( 'style.css', __FILE__ );
// 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.

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

Changelog

Since 2.6.0 Introduced.

plugins_url() code WP 6.9

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 );
}