wp_interactivity_state()
云策文档标注
概述
wp_interactivity_state() 函数用于获取和/或设置 Interactivity API 中指定命名空间的初始状态。如果状态已存在,新状态将与现有状态合并。
关键要点
- 函数用于管理 Interactivity API 存储的初始状态,支持获取和设置操作。
- 当状态已存在时,新提供的状态数组会与现有状态合并。
- 在派生状态获取器中,可以省略命名空间参数,默认使用定义获取器的命名空间。
- 参数 $store_namespace 为可选的唯一存储命名空间标识符,默认值为 null。
- 参数 $state 为可选的数组,将与指定存储命名空间的现有状态合并,默认值为空数组。
- 返回值是数组,表示指定存储命名空间的状态;如果提供了 $state 参数,则返回更新后的状态。
- 函数内部调用 wp_interactivity()->state() 实现功能。
- 自 WordPress 6.5.0 版本引入,6.6.0 版本允许在派生状态获取器中省略命名空间。
代码示例
function wp_interactivity_state( ?string $store_namespace = null, array $state = array() ): array {
return wp_interactivity()->state( $store_namespace, $state );
}
原文内容
Gets and/or sets the initial state of an Interactivity API store for a given namespace.
Description
If state for that store namespace already exists, it merges the new provided state with the existing one.
The namespace can be omitted inside derived state getters, using the namespace where the getter is defined.
Parameters
$store_namespacestringoptional-
The unique store namespace identifier.
Default:
null $statearrayoptional-
The array that will be merged with the existing state for the specified store namespace.
Default:
array()
Source
function wp_interactivity_state( ?string $store_namespace = null, array $state = array() ): array {
return wp_interactivity()->state( $store_namespace, $state );
}