在创建设置页面时,每个新的设置字段都需要单独使用register_setting()注册,或者我可以将设置部分的段塞传递给它来注册字段吗?
发布于 2023-01-05 07:52:34
函数注册一个选项或设置组。因此,您可以注册节的组并使用该组中的所有字段,因为该组将所有设置字段存储为一个项中的数组。
register_setting(
'_example_plugin_settings',
'_example_object_settings',
array(
'type' => 'object',
'default' => array(
'some_str' => 'A',
'some_int' => 3,
),
)
);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'),然后获取此项的所有内容,如下
$options = get_option('_example_plugin_settings');
print_r($options['some_text_field']);函数的文档也有一些有用的例子。
https://wordpress.stackexchange.com/questions/412596
复制相似问题