本文档介绍了 WordPress 核心 preferences 命名空间,用于管理用户偏好的状态和操作。它提供了选择器和动作来获取、设置、切换偏好,并支持持久化层配置。
// 示例:使用 get 选择器检查偏好
const isEnabled = get(state, 'core/edit-post', 'featureName');
// 示例:使用 set 动作设置偏好
dispatch(set('core/edit-post', 'preferenceName', true));
// 示例:使用 setPersistenceLayer 配置持久化层
dispatch(setPersistenceLayer(persistenceLayer));Namespace: core/preferences.
Returns a boolean indicating whether a prefer is active for a particular scope.
Parameters
StoreState: The store state.string: The scope of the feature (e.g. core/edit-post).string: The name of the feature.Returns
*: Is the feature enabled?Returns an action object used in signalling that a preference should be set to a value
Parameters
string: The preference scope (e.g. core/edit-post).string: The preference name.*: The value to set.Returns
SetAction: Action object.Returns an action object used in signalling that preference defaults should be set.
Parameters
string: The preference scope (e.g. core/edit-post).ScopedDefaults: A key/value map of preference names to values.Returns
SetDefaultsAction: Action object.Sets the persistence layer.
When a persistence layer is set, the preferences store will:
get immediately and update the store state to the value returned.set with all preferences whenever a preference changes value.setPersistenceLayer should ideally be dispatched at the start of an application’s lifecycle, before any other actions have been dispatched to the preferences store.
Parameters
WPPreferencesPersistenceLayer< D >: The persistence layer.Returns
Promise< SetPersistenceLayerAction< D > >: Action object.Returns an action object used in signalling that a preference should be toggled.
Parameters
string: The preference scope (e.g. core/edit-post).string: The preference name.