函数文档

wp_get_speculation_rules_configuration()

💡 云策文档标注

概述

wp_get_speculation_rules_configuration() 函数用于获取 WordPress 中推测规则(speculative loading)的配置。它返回一个包含 'mode' 和 'eagerness' 键的关联数组,或 null 表示禁用推测加载。

关键要点

  • 函数返回推测规则的配置数组或 null,默认仅在未登录用户且启用了固定链接时启用。
  • 配置包括 'mode'(模式,如 'prefetch' 或 'prerender')和 'eagerness'(急切程度,如 'eager'、'moderate' 或 'conservative')。
  • 通过 'wp_speculation_rules_configuration' 过滤器可以自定义配置,允许开发者覆盖默认行为。
  • 函数内部会进行配置验证和清理,确保返回有效的值,例如将 'auto' 替换为默认值。

代码示例

// 示例:获取推测规则配置
$config = wp_get_speculation_rules_configuration();
if ( $config ) {
    echo 'Mode: ' . $config['mode'] . ', Eagerness: ' . $config['eagerness'];
} else {
    echo 'Speculative loading is disabled.';
}

注意事项

  • 默认情况下,推测加载仅对未登录用户且启用了固定链接的站点启用,否则返回 null。
  • 配置中的 'mode' 和 'eagerness' 值必须有效,否则会被替换为默认值('prefetch' 和 'conservative')。
  • 使用过滤器时,可以传递 null 来完全禁用推测加载,或自定义数组以调整行为。

📄 原文内容

Returns the speculation rules configuration.

Return

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

Source

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.
	$default_mode      = 'prefetch';
	$default_eagerness = 'conservative';
	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'],
	);
}

Hooks

apply_filters( ‘wp_speculation_rules_configuration’, array<string,  )

Filters the way that speculation rules are configured.

Changelog

Version Description
6.8.0 Introduced.