我正在制作一个使用设置API的管理页面插件。我可以做一个部分和一个选项。但如何向其添加其他表单字段?
如果我向回调函数添加输入标记,它会将其添加到同一字段。这是否意味着如果要添加另一个字段,是否必须创建另一个函数?我认为这对于保存一个选项值来说太多了。所以肯定不是这样。
那么,您能否修改这个非常简单的示例插件,以添加另一个表单选项?此外,我还想添加另一个部分。
<?php
/*
Plugin Name: Settings API Demo
Description: Learning Setting Field and Settings Section
Author: Teno
*/
add_action(\'admin_init\', \'settingsapi_init\');
function settingsapi_init(){
register_setting( \'settingsapi_optiongroupname\', \'settingsapi_optionname\');
add_settings_section(\'plugin_main\', \'Section 1\', \'settingsapi_sectiondescription\', \'settingsapi_pageslug\');
add_settings_field(\'plugin_text_string\', \'Option A\', \'settingsapi_setting_string\', \'settingsapi_pageslug\', \'plugin_main\');
add_settings_field(\'plugin_text_string\', \'Option B\', \'settingsapi_setting_string\', \'settingsapi_pageslug\', \'plugin_main\');
}
function settingsapi_sectiondescription() {
echo \'<p>This is a section description.</p>\';
}
function settingsapi_setting_string() {
$options = get_option(\'settingsapi_optionname\');
echo "<input id=\'plugin_text_string\' name=\'settingsapi_optionname[option_a]\' size=\'40\' type=\'text\' value=\'{$options[\'option_a\']}\' />";
// echo "<input id=\'plugin_text_string\' name=\'settingsapi_optionname[option_b]\' size=\'40\' type=\'text\' value=\'{$options[\'option_b\']}\' />"; // <-- this is not what I want.
}
// admin menu
add_action(\'admin_menu\', \'plugin_admin_add_page\');
function plugin_admin_add_page() {
add_options_page(\'Custom Plugin Page\', \'Demo Settings API Menu\', \'manage_options\', \'settingsapi_pageslug\', \'settingsapi_adminpage\');
}
function settingsapi_adminpage() {
?>
<div class="wrap">
<h2>Demo Plugin for Settings API</h2>
<form action="options.php" method="post">
<?php settings_fields(\'settingsapi_optiongroupname\'); ?>
<?php do_settings_sections(\'settingsapi_pageslug\'); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
?>
最合适的回答,由SO网友:Mamaduka 整理而成
您对两个设置字段使用相同的回调函数。使用不同的输入。以下是更新的代码:
<?php
/*
Plugin Name: Settings API Demo
Description: Learning Setting Field and Settings Section
Author: Teno
*/
add_action(\'admin_init\', \'settingsapi_init\');
function settingsapi_init(){
register_setting( \'settingsapi_optiongroupname\', \'settingsapi_optionname\');
add_settings_section(\'plugin_main\', \'Section 1\', \'settingsapi_sectiondescription\', \'settingsapi_pageslug\');
add_settings_field(\'plugin_text_string_a\', \'Option A\', \'settingsapi_setting_string_a\', \'settingsapi_pageslug\', \'plugin_main\');
add_settings_field(\'plugin_text_string_b\', \'Option B\', \'settingsapi_setting_string_b\', \'settingsapi_pageslug\', \'plugin_main\');
}
function settingsapi_sectiondescription() {
echo \'<p>This is a section description.</p>\';
}
// First field callback.
function settingsapi_setting_string_a() {
$options = get_option(\'settingsapi_optionname\');
echo "<input id=\'plugin_text_string\' name=\'settingsapi_optionname[option_a]\' size=\'40\' type=\'text\' value=\'{$options[\'option_a\']}\' />";
}
// Second field callback.
function settingsapi_setting_string_b(){
$options = get_option(\'settingsapi_optionname\');
echo "<input id=\'plugin_text_string\' name=\'settingsapi_optionname[option_b]\' size=\'40\' type=\'text\' value=\'{$options[\'option_b\']}\' />";
}
// admin menu
add_action(\'admin_menu\', \'plugin_admin_add_page\');
function plugin_admin_add_page() {
add_options_page(\'Custom Plugin Page\', \'Demo Settings API Menu\', \'manage_options\', \'settingsapi_pageslug\', \'settingsapi_adminpage\');
}
function settingsapi_adminpage() {
?>
<div class="wrap">
<h2>Demo Plugin for Settings API</h2>
<form action="options.php" method="post">
<?php settings_fields(\'settingsapi_optiongroupname\'); ?>
<?php do_settings_sections(\'settingsapi_pageslug\'); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
?>