钩子文档

allowed_options

💡 云策文档标注

概述

allowed_options 是一个 WordPress 过滤器,用于修改允许的选项列表。它允许开发者自定义哪些选项可以被保存或处理,通常用于扩展设置页面或控制选项权限。

关键要点

  • allowed_options 过滤器用于过滤允许的选项列表,参数为 $allowed_options 数组。
  • 通过 add_filter 钩子可以添加自定义选项到允许列表中,示例代码展示了如何基于数组结构动态添加选项。
  • 此过滤器自 WordPress 5.5.0 版本引入,是核心功能的一部分。

代码示例

$sections = array(
    'section-one' => array(
        'option-one',
        'option-two'
    ),
    'section-two' => array(
        'option-one',
        'option-two'
    ),
);
 
add_filter('allowed_options', function($allowed_options) use ($sections) {
    foreach($sections as $section => $fields) {
        foreach($fields as $field) {
            $allowed_options[$section][] = $field;
        }
    }
    return $allowed_options;
});

📄 原文内容

Filters the allowed options list.

Parameters

$allowed_optionsarray
The allowed options list.

Source

$allowed_options = apply_filters( 'allowed_options', $allowed_options );

Changelog

Version Description
5.5.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example how to add allowed_options.

    (Adapted deprecated code by arnoutpullen)

    $sections = array(
        'section-one' => array(
            'option-one',
            'option-two'
        ),
        'section-two' => array(
            'option-one',
            'option-two'
        ),
    );
     
    add_filter('allowed_options', function($allowed_options) use ($sections) {
        foreach($sections as $section => $fields) {
            foreach($fields as $field) {
                $allowed_options[$section][] = $field;
            }
        }
        return $allowed_options;
    });