钩子文档

wp_speculation_rules_configuration

💡 云策文档标注

概述

本文档介绍 WordPress 中的 wp_speculation_rules_configuration 过滤器,用于配置 Speculation Rules API 的行为,以控制 URL 的预取或预渲染模式及加载积极性。

关键要点

  • wp_speculation_rules_configuration 过滤器允许开发者自定义 Speculation Rules API 的配置,包括模式(prefetch 或 prerender)和积极性(eager、moderate 或 conservative)。
  • 默认配置由 WordPress Core 决定(auto),但可通过此过滤器强制设置特定值或完全禁用推测性加载(通过返回 null)。
  • 对于已登录用户或未使用固定链接的站点,默认值为 null,表示推测性加载被禁用。

代码示例

$config = apply_filters( 'wp_speculation_rules_configuration', $config );

注意事项

  • 参数 $config 是一个关联数组,包含 'mode' 和 'eagerness' 键,或为 null;默认值均为 'auto'。
  • 更多信息可参考 Chrome 开发者文档中的 Prerender Pages 部分。

📄 原文内容

Filters the way that speculation rules are configured.

Description

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.

See also

Parameters

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.

Source

$config = apply_filters( 'wp_speculation_rules_configuration', $config );

Changelog

Version Description
6.8.0 Introduced.