wppaste
WordPress

wp_get_speculation_rules_configuration(): array<string,

Since
6.8.0, 7.1.0
Source
wp-includes/speculative-loading.php:26
Returns the speculation rules configuration.

Compatibility

WordPress
since 7.1.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 7.1.0, and compiles on PHP 7.4 through 8.6-dev.

Return value

array<string,
string>|null Associative array with 'mode' and 'eagerness' keys, or null if speculative loading is disabled.

Performance profile

How much work a call to wp_get_speculation_rules_configuration() 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
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

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

Instructions
12–57

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

Plugin surface
1 hook

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

Called by
1

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

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below wp_get_speculation_rules_configuration()
  • serializeserialisationmaybe_unserialize()one call below wp_get_speculation_rules_configuration()

Further down the call graph this can also reach 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 · 8 distinct outcomes

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

WhenInstructionsCalls it makes
is_user_logged_in() && $config === null12is_user_logged_in(), apply_filters()
!is_user_logged_in() && $config === null16–17is_user_logged_in(), get_option(), apply_filters()
is_user_logged_in() && $config !== null21–37is_user_logged_in(), apply_filters(), wp_get_speculation_rules_default_configuration()
!is_user_logged_in() && $config !== null25–42is_user_logged_in(), get_option(), apply_filters(), wp_get_speculation_rules_default_configuration()
is_user_logged_in() && $config !== null && is_array($config) && isset($config)38–46is_user_logged_in(), apply_filters(), wp_get_speculation_rules_default_configuration(), ::WP_Speculation_Rules()
!is_user_logged_in() && $config !== null && is_array($config) && isset($config)42–51is_user_logged_in(), get_option(), apply_filters(), wp_get_speculation_rules_default_configuration(), ::WP_Speculation_Rules()
is_user_logged_in() && $config !== null && is_array($config) && isset($config)47–52is_user_logged_in(), apply_filters(), wp_get_speculation_rules_default_configuration(), ::WP_Speculation_Rules(), ::WP_Speculation_Rules()
!is_user_logged_in() && $config !== null && is_array($config) && isset($config)51–57is_user_logged_in(), get_option(), apply_filters(), wp_get_speculation_rules_default_configuration(), ::WP_Speculation_Rules(), ::WP_Speculation_Rules()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 62 instructions, 12–57 executed per call, 11 branches. The work does not change between versions.

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 wp_get_speculation_rules_configuration() runs, in this order:

  1. apply_filters( wp_speculation_rules_configuration )filterline 62 (+36 into the body)

    Filters the way that speculation rules are configured.

Uses · 6

Used by · 1

Source code

function wp_get_speculation_rules_configuration(): ?array {	// By default, speculative loading is only enabled for sites with pretty permalinks when no user is logged in.	if ( ! is_user_logged_in() && get_option( 'permalink_structure' ) ) {		$config = array(			'mode'      => 'auto',			'eagerness' => 'auto',		);	} else {		$config = null;	} 	/**	 * Filters the way that speculation rules are configured.	 *	 * The Speculation Rules API is a web API that allows to automatically prefetch or prerender certain URLs on the	 * page, which can lead to near-instant page load times. This is also referred to as speculative loading.	 *	 * There are two aspects to the configuration:	 * * The "mode" (whether to "prefetch" or "prerender" URLs).	 * * The "eagerness" (whether to speculatively load URLs in an "eager", "moderate", or "conservative" way).	 *	 * By default, the speculation rules configuration is decided by WordPress Core ("auto"). This filter can be used	 * to force a certain configuration, which could for instance load URLs more or less eagerly.	 *	 * For logged-in users or for sites that are not configured to use pretty permalinks, the default value is `null`,	 * indicating that speculative loading is entirely disabled.	 *	 * @since 6.8.0	 * @see https://developer.chrome.com/docs/web-platform/prerender-pages	 *	 * @param array<string, string>|null $config Associative array with 'mode' and 'eagerness' keys, or `null`. The	 *                                           default value for both of the keys is 'auto'. Other possible values	 *                                           for 'mode' are 'prefetch' and 'prerender'. Other possible values for	 *                                           'eagerness' are 'eager', 'moderate', and 'conservative'. The value	 *                                           `null` is used to disable speculative loading entirely.	 */	$config = apply_filters( 'wp_speculation_rules_configuration', $config ); 	// Allow the value `null` to indicate that speculative loading is disabled.	if ( null === $config ) {		return null;	} 	// Sanitize the configuration and replace 'auto' with current defaults.	$defaults          = wp_get_speculation_rules_default_configuration();	$default_mode      = $defaults['mode'];	$default_eagerness = $defaults['eagerness']; 	if ( ! is_array( $config ) ) {		return array(			'mode'      => $default_mode,			'eagerness' => $default_eagerness,		);	}	if (		! isset( $config['mode'] ) ||		'auto' === $config['mode'] ||		! WP_Speculation_Rules::is_valid_mode( $config['mode'] )	) {		$config['mode'] = $default_mode;	}	if (		! isset( $config['eagerness'] ) ||		'auto' === $config['eagerness'] ||		! WP_Speculation_Rules::is_valid_eagerness( $config['eagerness'] ) ||		// 'immediate' is a valid eagerness, but for safety WordPress does not allow it for document-level rules.		'immediate' === $config['eagerness']	) {		$config['eagerness'] = $default_eagerness;	} 	return array(		'mode'      => $config['mode'],		'eagerness' => $config['eagerness'],	);}

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.

7.1.0
The WP_SPECULATIVE_LOADING_DEFAULT_MODE and WP_SPECULATIVE_LOADING_DEFAULT_EAGERNESS constants and environment variables can now specify the default mode and eagerness, respectively.from the docblock
6.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/speculative-loading.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.