Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am certain it is something silly. But I can't find it and with such simple code at a loss for anything else. Having looked at many other plugins doing settings in a lot of ways can't really see how it is different from those doing similar code. This is actually the select code from WordPress Codex. After my code didn't work I went and checked the codex and tried it. The really odd thing is that occasionally WordPress saves the selection. But mostly it doesn't.

I can bypass the WordPress methods and use straight PHP, but then I learn nothing. Many plugins are bypassing the register_settings method to deal with POST and DB.

Again as last time setup as a plugin. As always your help is greatly appreciated.

    <?php
    /*
    Plugin Name: myplug
    Version: 0.1
    Plugin URI: http://myplug.org
    Author: ME
    Description: Stupid plugin
     */

    add_action('admin_init', 'myplug_register_options');  // register options for the form
    add_action('admin_menu', 'myplug_admin_links');  // register admin menu hyperlinks

    /** Function to register form fields **/
    function myplug_register_options(){
            register_setting('myplug_options_group', 'myplug_settings', 'myplug_validate');
    }

    /** Function to add hyperlinks to the admin menus using hooks and filters. **/
    function myplug_admin_links() {
      add_options_page('myplug Setup', 'myplug', 'manage_options', 'myplug', 'myplug_admin_page' );  // add link to settings page
      add_filter( 'plugin_action_links', 'myplug_settings_link', 10, 2 );  // add link to plugin page
    }

    /** Function to create link for plugin_action_links filter **/
    function myplug_settings_link($links, $file){
            if ( $file == plugin_basename( dirname(__FILE__). '/myplug.php')){
                    $settings_link = '<a href="options-general.php?page=myplug">' .__('Settings') . '</a>';
                    array_unshift( $links, $settings_link ); // place before other links
            }
    return $links;
    }

    /** Validate User Input **/
    function myplug_validate($input) {
            $input['select_it'] = wp_filter_nohtml_kses($input['select_it']);
    return($input);
    }

    /** Draw the Settings Page **/
    function myplug_admin_page(){
    ?>
    <div>
      <h2>Options</h2>
      <form method="post" action="options.php">
      <?php settings_fields('myplug_options_group'); ?>
      <?php $myplug_options = get_option('myplug_settings'); ?>
        <select name="input[select_it]">
            <option value="1" <?php selected( $input['select_it'], 1 ); ?>>1</option>
            <option value="2" <?php selected( $input['select_it'], 2 ); ?>>2</option>
            <option value="3" <?php selected( $input['select_it'], 3 ); ?>>3</option>
        </select>
      <?php submit_button(); ?>
      </form>
    </div>
    <?php
    }
    ?>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

See the myplug_admin_page function.

And see the selected calls inside it. selected works in a way that check the value of the item with the current saved value.

In your selected calls the current saved value should be $input['select_it'] but.. no $input variable it's defined in that function.

In that function the current saved value is in the var $myplug_options (because of the $myplug_options = get_option('myplug_settings')).

So in selected calls replace $input with $myplug_options and I bet it will works.

share|improve this answer
    
LOL...ROFLMAO!!! Hysterical! OMG! I can't believe I didn't catch another stupid typo. In this code that was completely by mistake and my eyes were tired. I checked the production code and lo and behold I had used the wrong name! I was using $var_settings instead of $var_options and for some reason my mind never caught it! Thank you very much Guiseppe! It's too bad I can't have you working next to me. I would be much more productive with your eyes catching these silly mistakes! –  Mark Finch Sep 14 '13 at 15:07
    
LOL I was just thinking I knew it was something silly but I wasted a whole day looking for it! So all that drama on the last comment at the start is kinda overkill! :-D But too many posts/comments don't elate feelings so I will show mine! Thanks again Guiseppe! –  Mark Finch Sep 14 '13 at 15:13
    
You are welcome, @MarkFinch ;) –  G. M. Sep 15 '13 at 21:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.