On this page
plugins_url( string $path = '', string $plugin = '' ): string
Retrieves a URL within the plugins or mu-plugins directory.
Description
Defaults to the plugins directory URL if no arguments are supplied.
Parameters
$pathstring Optional-
Extra path appended to the end of the URL, including the relative directory if $plugin is supplied.
Default:
'' $pluginstring Optional-
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:
''
Return
string Plugins URL link with optional paths appended.
More Information
This function retrieves the absolute URL to the plugins or mu-plugins directory (without the trailing slash) or, when using the $path argument, to a specific file under that directory. You can either specify the $path argument as a hardcoded path relative to the plugins or mu-plugins directory, or conveniently pass __FILE__ as the second argument to make the $path relative to the parent directory of the current PHP script file.
Uses the WP_PLUGIN_URL or, in the case the $plugin path begins with the WPMU_PLUGIN_DIR path, the WPMU_PLUGIN_URL constant internally, to compose the resultant URL. Note that the direct usage of WordPress internal constants is not recommended.
Uses apply_filters() to apply “plugins_url” filters on the resultant URL, with the following line of code
return apply_filters( 'plugins_url', $url, $path, $plugin );
The plugins_url() function should not be called in the global context of plugins, but rather in a hook like “init” or “admin_init” to ensure that the “plugins_url” filters are already hooked at the time the function is called. This is vital for many site configurations to work, and if plugins_url() is called in the global context of a plugin file it cannot be filtered by other plugins (though mu-plugins are able to filter it because they run before any other plugins).
Source
File: wp-includes/link-template.php. View all references
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 ) && 0 === strpos( $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 );
}
Hooks
- apply_filters( 'plugins_url',
string $url ,string $path ,string $plugin ) -
Filters the URL to the plugins directory.
Related
Uses
| Uses | Description |
|---|---|
| wp_normalize_path() wp-includes/functions.php | Normalizes a filesystem path. |
| set_url_scheme() wp-includes/link-template.php | Sets the scheme for a URL. |
| plugin_basename() wp-includes/plugin.php | Gets the basename of a plugin. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| register_block_script_handle() wp-includes/blocks.php | Finds a script handle for the selected block metadata field. It detects when a path to file was provided and finds a corresponding asset file with details necessary to register the script under automatically generated handle name. It returns unprocessed script handle otherwise. |
| register_block_style_handle() wp-includes/blocks.php | Finds a style handle for the block metadata field. It detects when a path to file was provided and registers the style under automatically generated handle name. It returns unprocessed style handle otherwise. |
| load_script_textdomain() wp-includes/l10n.php | Loads the script translated strings. |
| get_theme_root_uri() wp-includes/theme.php | Retrieves URI for themes directory. |
| plugin_dir_url() wp-includes/plugin.php | Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in. |
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/plugins_url