首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用register_setting()

如何使用register_setting()
EN

WordPress Development用户
提问于 2023-01-05 03:10:34
回答 1查看 27关注 0票数 0

在创建设置页面时,每个新的设置字段都需要单独使用register_setting()注册,或者我可以将设置部分的段塞传递给它来注册字段吗?

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2023-01-05 07:52:34

函数注册一个选项或设置组。因此,您可以注册节的组并使用该组中的所有字段,因为该组将所有设置字段存储为一个项中的数组。

代码语言:javascript
复制
register_setting(
    '_example_plugin_settings',
    '_example_object_settings',
    array(
        'type'         => 'object',
        'default'      => array(
            'some_str' => 'A',
            'some_int' => 3,
        ),
    )
);
代码语言:javascript
复制
add_action( 'admin_init', 'example_register_settings' );
/**
 * One settings group with two items.
 */
function example_register_settings() {

  register_setting(
    '_example_plugin_settings',
    '_example_object_settings',
    '_validate_example_plugin_settings'
  );

  add_settings_section(
    'section_one',
    'Section One',
    '_section_one_text',
    '_example_plugin'
  );

  add_settings_field(
    'some_text_field',
    'Some Text Field',
    '_render_some_text_field',
    '_example_plugin',
    'section_one'
  );
  add_settings_field(
    'another_number_field',
    'Another Number Field',
    '_render_another_number_field',
    '_example_plugin',
    'section_one'
  );

}

如果获得设置条目,请使用get_option('_example_plugin_settings'),然后获取此项的所有内容,如下

代码语言:javascript
复制
$options = get_option('_example_plugin_settings');
print_r($options['some_text_field']);

函数的文档也有一些有用的例子。

票数 2
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/412596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档