简体   繁体   English

如何在WordPress插件中保存选择选项下拉菜单

[英]how to save a select option dropdown in a wordpress plugin

i have the following code that is taken from the WordPress codex on how to save a simple text input field. 我有以下代码取自WordPress Codex,以了解如何保存简单的文本输入字段。 it works fine. 它工作正常。 but i am trying to now save a select option dropdown and can't figure out what i am missing here. 但我现在尝试保存选择选项下拉列表,但无法弄清楚我在这里缺少什么。 it seems it saves the first time i hit save from the dropdown, but when i try to edit the value to select another option and save it, it never saves. 似乎它保存了我第一次从下拉列表中单击“保存”的时间,但是当我尝试编辑该值以选择另一个选项并保存它时,它将永远不会保存。

my other queston is why is the value not being printed to the screen when i run 我的另一个问题是,为什么我在跑步时没有将值打印到屏幕上

testing value saved: <?=esc_attr( get_option('location_one_option') ); ?>

the current plugin code is below: 当前的插件代码如下:

// 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 }

Use this function this is just an example to save a select option dropdown in a wordpress plugin you can use it as per your needed. 使用此功能仅是将选择选项下拉列表保存在wordpress插件中的示例,您可以根据需要使用它。

Example 1 例子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();

Retrieve this value 检索该值

$dropdown_option = get_option( 'dropdown_option_setting_option_name' ); // Array
$dropdown_value =  $dropdown_option ['dropdown_option_0']; // Option value

If you need a simple way to do this 如果您需要一种简单的方法来执行此操作

Example 2 例子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
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM