钩子文档

rest_pre_update_setting

💡 云策文档标注

概述

rest_pre_update_setting 是一个 WordPress 过滤器,用于在通过 REST API 更新设置值时进行预占,允许开发者覆盖默认更新逻辑。

关键要点

  • 过滤器允许通过返回 true 来劫持设置更新过程,从而自定义行为。
  • 参数包括 $result(是否覆盖默认行为)、$name(设置名称)、$value(更新值)和 $args(register_setting() 的参数)。
  • $args 包含类型、标签、描述、sanitize_callback、show_in_rest 和默认值等详细信息。

代码示例

$updated = apply_filters( 'rest_pre_update_setting', false, $name, $request[ $name ], $args );

注意事项

  • 此过滤器在 WordPress 4.7.0 版本中引入。
  • 主要用于 WP_REST_Settings_Controller::update_item() 中,以控制设置更新。

📄 原文内容

Filters whether to preempt a setting value update via the REST API.

Description

Allows hijacking the setting update logic and overriding the built-in behavior by returning true.

Parameters

$resultbool
Whether to override the default behavior for updating the value of a setting.
$namestring
Setting name (as shown in REST API responses).
$valuemixed
Updated setting value.
$argsarray
Arguments passed to register_setting() for this setting.

More Arguments from register_setting( … $args )

Data used to describe the setting when registered.

  • type string
    The type of data associated with this setting.
    Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
  • label string
    A label of the data attached to this setting.
  • description string
    A description of the data attached to this setting.
  • sanitize_callback callable
    A callback function that sanitizes the option’s value.
  • show_in_rest bool|array
    Whether data associated with this setting should be included in the REST API.
    When registering complex settings, this argument may optionally be an array with a 'schema' key.
  • default mixed
    Default value when calling get_option().

Source

$updated = apply_filters( 'rest_pre_update_setting', false, $name, $request[ $name ], $args );

Changelog

Version Description
4.7.0 Introduced.