为了保持提交的值,需要将其保存到相应的选项数组中。为此,可以添加另一个函数(pe_save_settings()
在本例中)捕获表单提交并保存字段内容。以下是完整的工作代码:
<?php
/*
Plugin Name: Test option page
Text Domain: test-option-page
*/
function reg_settings(){
register_setting(\'pe_theme_options\',\'pe_theme_options_item\');
add_settings_section(\'pe_main_settings\',\'Main Settings\', \'\', __FILE__);
add_settings_field(\'pe_bannar_heading\', \'Bannar Heading:\', \'pe_bannar_heading_setting\', __FILE__, \'pe_main_settings\');
}
add_action(\'admin_init\',\'reg_settings\');
function pe_bannar_heading_setting() {
$options = get_option(\'pe_theme_options_array\'); ?>
<input id="pe_theme_options_item" name="pe_theme_options_item" type="text" value="<?php if ( isset( $options[\'pe_bannar_heading\'] ) ) { echo $options[\'pe_bannar_heading\']; } ?>"/>
<?php
}
function pe_add_menu_page(){
add_options_page(\'Theme Options\', \'Theme Options\', \'administrator\', __FILE__ , \'display_options_page\');
}
add_action(\'admin_menu\',\'pe_add_menu_page\');
function display_options_page() {
$options = get_option(\'pe_theme_options_array\');?>
<div class="wrap">
<h2>Yellow Blog Options</h2>
<form action="options.php" method="post" enctype="multipart/form-data">
<?php
settings_fields(\'pe_theme_options\');
do_settings_sections(__FILE__);
submit_button( \'Save Settings\', \'primary\', \'pe_new_option\' );
?>
</form>
</div>
<?php }
add_action( \'admin_init\', \'pe_save_settings\' );
function pe_save_settings() {
if ( isset( $_POST[\'pe_new_option\'] ) ) {
$new_value = trim( $_POST[ \'pe_theme_options_item\'] );
$options_array = get_option(\'pe_theme_options_array\');
$options_array[\'pe_bannar_heading\'] = $new_value;
update_option( \'pe_theme_options_array\', $options_array );
}
}