wppaste
WordPress

plugins_url( string $path = '', string $plugin = '' ): string

Since
2.6.0
Source
wp-includes/link-template.php:3676
Retrieves a URL within the plugins or mu-plugins directory.

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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
32–57

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 58.

Plugin surface
1 hook

Third-party callbacks on 'plugins_url' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always32–39wp_normalize_path(), wp_normalize_path(), wp_normalize_path(), set_url_scheme(), apply_filters()
is_string($path)40–45wp_normalize_path(), wp_normalize_path(), wp_normalize_path(), set_url_scheme(), ltrim(), apply_filters()
!empty($plugin) && is_string($plugin) && $folder === "."40–45wp_normalize_path(), wp_normalize_path(), wp_normalize_path(), set_url_scheme(), plugin_basename(), apply_filters()
!empty($plugin) && is_string($plugin)46–51wp_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–57wp_normalize_path(), wp_normalize_path(), wp_normalize_path(), set_url_scheme(), plugin_basename(), ltrim(), ltrim(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5832–577
8.55832–577
8.45832–5776 fewer instructions than PHP 8.3
8.36432–637
8.26432–637
8.16432–637
7.46432–637

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:

  1. apply_filters( plugins_url )filterline 3712 (+36 into the body)

    Filters the URL to the plugins directory.

Uses · 5

Used by · 4

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.