plugins_url( string $path = '', string $plugin = '' ): string
- Since
- 2.6.0
- Source
wp-includes/link-template.php:3675
Description
Defaults to the plugins directory URL if no arguments are supplied.
Compatibility
- WordPress
- since 2.6.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$pathstringoptional- Extra path appended to the end of the URL, including the relative directory if $plugin is supplied. Default empty.Default:
'' $pluginstringoptional- A full path to a file inside a plugin or mu-plugin.
The URL will be relative to its directory. Default empty.
Typically this is done by passing__FILE__as the argument.Default:''
Return value
string- Plugins URL link with optional paths appended.
Performance profile
How much work a call to plugins_url() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 32–57
- Plugin surface
- 1 hook
- Called by
- 5
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 58.
Third-party callbacks on 'plugins_url' run inside this call, and their cost is not bounded by anything here.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost plugins_url() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 32–39 | wp_normalize_path(), wp_normalize_path(), wp_normalize_path(), set_url_scheme(), apply_filters() |
is_string($path) | 40–45 | wp_normalize_path(), wp_normalize_path(), wp_normalize_path(), set_url_scheme(), ltrim(), apply_filters() |
!empty($plugin) && is_string($plugin) && $folder === "." | 40–45 | wp_normalize_path(), wp_normalize_path(), wp_normalize_path(), set_url_scheme(), plugin_basename(), apply_filters() |
!empty($plugin) && is_string($plugin) | 46–51 | wp_normalize_path(), wp_normalize_path(), wp_normalize_path(), set_url_scheme(), plugin_basename(), ltrim(), apply_filters() |
!empty($plugin) && is_string($plugin) && $folder !== "." && is_string($path) | 54–57 | wp_normalize_path(), wp_normalize_path(), wp_normalize_path(), set_url_scheme(), plugin_basename(), ltrim(), ltrim(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 58 | 32–57 | 7 | |
| 8.5 | 58 | 32–57 | 7 | |
| 8.4 | 58 | 32–57 | 7 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 64 | 32–63 | 7 | |
| 8.2 | 64 | 32–63 | 7 | |
| 8.1 | 64 | 32–63 | 7 | |
| 7.4 | 64 | 32–63 | 7 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while plugins_url() runs, in this order:
- apply_filters( plugins_url )filterline 3711 (+36 into the body)
Filters the URL to the plugins directory.
Uses · 5
- wp_normalize_path()Normalizes a filesystem path.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- set_url_scheme()Sets the scheme for a URL.
- plugin_basename()Gets the basename of a plugin.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 5
- WP_URL_Pattern_Prefixer::get_default_contexts()Returns the default contexts used by the class.
- get_block_asset_url()Gets the URL to a block asset.
- get_theme_root_uri()Retrieves URI for themes directory.
- load_script_textdomain()Loads the script translated strings.
- plugin_dir_url()Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.
Source code
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 );}Changelog
Introduced in 2.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/link-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.