我有以下代码从WordPress codex中获取如何保存简单的文本输入字段。它工作正常。但我现在尝试保存一个选择选项下拉列表,并且无法弄清楚我在这里缺少什么。它似乎保存第一次我从下拉列表中保存,但当我尝试编辑该值以选择另一个选项并保存时,它永远不会保存。
我的另一个问题是为什么在我运行
时没有将值打印到屏幕上testing value saved: <?=esc_attr( get_option('location_one_option') ); ?>
当前的插件代码如下:
// create custom plugin settings menu
function sidebar_posts_create_menu() {
//create new top-level menu
add_menu_page('Sidebar Posts', 'Sidebar Post Settings', 'administrator', __FILE__, 'sidebar_posts_settings_page' );
//call register settings function
add_action( 'admin_init', 'register_sidebar_posts_settings' );
}
add_action('admin_menu', 'sidebar_posts_create_menu');
//register our settings
function register_sidebar_posts_settings() {
//register our settings
register_setting( 'sidebar-posts-settings-group', 'location_one_option' );
register_setting( 'sidebar-posts-settings-group', 'new_option_name' );
}
//register settings page view
function sidebar_posts_settings_page() {
?>
<div class="wrap">
<h1>Sidebar Posts Settings</h1>
<p>Select up to six posts to show in six different locations on the sidebar. Have fun!</p>
<form method="post" action="options.php">
<?php settings_fields( 'sidebar-posts-settings-group' ); ?>
<?php do_settings_sections( 'sidebar-posts-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">New Option Name</th>
<td><input type="text" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td>
</tr>
<!--the above simple text field saves the data perfectly-->
<?php
$your_query = new WP_Query( 'posts_per_page=-1' ); ?>
<label>Location One Post</label><br />
<select name="location_one_option">
<option value="" selected="selected">Select a post</option>
<?php while ( $your_query->have_posts() ) : $your_query->the_post(); ?>
<option value="<?=esc_attr( get_option('location_one_option') ); ?>" <?php selected( get_option('location_one_option'), get_option('location_one_option') ); ?>>
<?=the_title(); ?>
</option>
<? endwhile; ?>
</select>
<? wp_reset_postdata(); ?><br /><br />
testing value saved: <?=esc_attr( get_option('location_one_option') ); ?>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
答案 0 :(得分:2)
使用此功能这只是一个保存选项选项下拉列表的示例,您可以根据需要使用它。
示例1
class DropdownOptionSetting {
private $dropdown_option_setting_options;
public function __construct() {
add_action( 'admin_menu', array( $this, 'dropdown_option_setting_add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'dropdown_option_setting_page_init' ) );
}
public function dropdown_option_setting_add_plugin_page() {
add_options_page(
'Dropdown Option Setting', // page_title
'Dropdown Option Setting', // menu_title
'manage_options', // capability
'dropdown-option-setting', // menu_slug
array( $this, 'dropdown_option_setting_create_admin_page' ) // function
);
}
public function dropdown_option_setting_create_admin_page() {
$this->dropdown_option_setting_options = get_option( 'dropdown_option_setting_option_name' ); ?>
<div class="wrap">
<h2>Dropdown Option Setting</h2>
<p></p>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'dropdown_option_setting_option_group' );
do_settings_sections( 'dropdown-option-setting-admin' );
submit_button();
?>
</form>
</div>
<?php }
public function dropdown_option_setting_page_init() {
register_setting(
'dropdown_option_setting_option_group', // option_group
'dropdown_option_setting_option_name', // option_name
array( $this, 'dropdown_option_setting_sanitize' ) // sanitize_callback
);
add_settings_section(
'dropdown_option_setting_setting_section', // id
'Settings', // title
array( $this, 'dropdown_option_setting_section_info' ), // callback
'dropdown-option-setting-admin' // page
);
add_settings_field(
'dropdown_option_0', // id
'Dropdown Option', // title
array( $this, 'dropdown_option_0_callback' ), // callback
'dropdown-option-setting-admin', // page
'dropdown_option_setting_setting_section' // section
);
}
public function dropdown_option_setting_sanitize($input) {
$sanitary_values = array();
if ( isset( $input['dropdown_option_0'] ) ) {
$sanitary_values['dropdown_option_0'] = $input['dropdown_option_0'];
}
return $sanitary_values;
}
public function dropdown_option_setting_section_info() {
}
public function dropdown_option_0_callback() {
?> <select name="dropdown_option_setting_option_name[dropdown_option_0]" id="dropdown_option_0">
<?php $selected = (isset( $this->dropdown_option_setting_options['dropdown_option_0'] ) && $this->dropdown_option_setting_options['dropdown_option_0'] === 'option-one') ? 'selected' : '' ; ?>
<option value="option-one" <?php echo $selected; ?>>Option One</option>
<?php $selected = (isset( $this->dropdown_option_setting_options['dropdown_option_0'] ) && $this->dropdown_option_setting_options['dropdown_option_0'] === 'option-two') ? 'selected' : '' ; ?>
<option value="option-two" <?php echo $selected; ?>>Option Two</option>
<?php $selected = (isset( $this->dropdown_option_setting_options['dropdown_option_0'] ) && $this->dropdown_option_setting_options['dropdown_option_0'] === 'option-three') ? 'selected' : '' ; ?>
<option value="option-three" <?php echo $selected; ?>>Option Three</option>
</select> <?php
}
}
if ( is_admin() )
$dropdown_option_setting = new DropdownOptionSetting();
检索此值
$dropdown_option = get_option( 'dropdown_option_setting_option_name' ); // Array
$dropdown_value = $dropdown_option ['dropdown_option_0']; // Option value
如果您需要一种简单的方法来执行此操作
示例2
add_action( 'admin_init', 'Dropdown_settings_init' );
function Dropdown_settings_init( ) {
register_setting( 'pluginPage', 'dropdown_settings' );
add_settings_section(
'Dropdown_pluginPage_section',
__( 'Your section description', 'dropdown' ),
'Dropdown_settings_section_callback',
'pluginPage'
);
add_settings_field(
'select_field_0',
__( 'Settings field description', 'dropdown' ),
'Dropdown_select_field_render',
'pluginPage',
'Dropdown_pluginPage_section'
);
}
function Dropdown_select_field_render( ) {
$options = get_option( 'dropdown_settings' );
?>
<select name='dropdown_settings[select_field_0]'>
<option value='1' <?php selected( $options['select_field_0'], 1 ); ?>>Option 1</option>
<option value='2' <?php selected( $options['select_field_0'], 2 ); ?>>Option 2</option>
<option value='3' <?php selected( $options['select_field_0'], 3 ); ?>>Option 3</option>
<option value='4' <?php selected( $options['select_field_0'], 4 ); ?>>Option 4</option>
</select>
<?php
}
function Dropdown_settings_section_callback( ) {
echo __( 'This section description', 'dropdown' );
}
function Dropdown_options_page( ) {
?>
<form action='options.php' method='post'>
<h2>Dropdown</h2>
<?php
settings_fields( 'pluginPage' );
do_settings_sections( 'pluginPage' );
submit_button();
?>
</form>
<?php
}
?>