wppaste
WordPress

WP_URL_Pattern_Prefixer::prefix_path_pattern( string $path_pattern, string $context = 'home' ): string

Since
6.8.0
Source
wp-includes/class-wp-url-pattern-prefixer.php:64
Prefixes the given URL path pattern with the base path for the given context.

Description

This ensures that these path patterns work correctly on WordPress subdirectory sites, for example in a multisite network, or when WordPress itself is installed in a subdirectory of the hostname.

The given URL path pattern is only prefixed if it does not already include the expected prefix.

Compatibility

WordPress
since 6.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 4 of the 5 tracked releases, added in 6.8.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$path_patternstring
URL pattern starting with the path segment.
$contextstringoptional
Context to use for prefixing the path pattern. Default 'home'.Default: 'home'

Return value

string
URL pattern, prefixed as necessary.

Performance profile

How much work a call to WP_URL_Pattern_Prefixer::prefix_path_pattern() 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
21–31

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()one call below WP_URL_Pattern_Prefixer::prefix_path_pattern()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_URL_Pattern_Prefixer::prefix_path_pattern() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!isset($context)21__(), sprintf(), esc_html(), _doing_it_wrong()
isset($context)23–31strcspn(), ltrim()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev4721–313
8.54721–313
8.44721–3139 fewer instructions than PHP 8.3
8.35621–403
8.25621–4031 fewer instruction than PHP 8.1
8.15722–403
7.45722–403

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.

Uses · 4

Source code

	public function prefix_path_pattern( string $path_pattern, string $context = 'home' ): string {		// If context path does not exist, the context is invalid.		if ( ! isset( $this->contexts[ $context ] ) ) {			_doing_it_wrong(				__FUNCTION__,				esc_html(					sprintf(						/* translators: %s: context string */						__( 'Invalid URL pattern context %s.' ),						$context					)				),				'6.8.0'			);			return $path_pattern;		} 		/*		 * In the event that the context path contains a :, ? or # (which can cause the URL pattern parser to switch to		 * another state, though only the latter two should be percent encoded anyway), it additionally needs to be		 * enclosed in grouping braces. The final forward slash (trailingslashit ensures there is one) affects the		 * meaning of the * wildcard, so is left outside the braces.		 */		$context_path         = $this->contexts[ $context ];		$escaped_context_path = $context_path;		if ( strcspn( $context_path, ':?#' ) !== strlen( $context_path ) ) {			$escaped_context_path = '{' . substr( $context_path, 0, -1 ) . '}/';		} 		/*		 * If the path already starts with the context path (including '/'), remove it first		 * since it is about to be added back.		 */		if ( str_starts_with( $path_pattern, $context_path ) ) {			$path_pattern = substr( $path_pattern, strlen( $context_path ) );		} 		return $escaped_context_path . ltrim( $path_pattern, '/' );	}

Changelog

Introduced in 6.8.0. Unchanged from 6.8.8 through 7.1.0.

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

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

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-url-pattern-prefixer.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.