I broke my plugin Options page, please help me fix it
-
So I have this very simple plugin, which saves a single option using the standard options management functions WordPress offers.
It worked. And while maintaining it, I broke it, only to discover I can’t figure out how it was supposed to work.The good news: Settings page loads and is populated as expected. I can select an option from the dropdown list and press Save.
The bad news: When the Settings page reloads after save, the old value is restored.
So apparently, I broke the bit that saves the option.But as I understand it, that’s the bit that happens behind the scenes, based on my correct code.
So apparently, my code isn’t entirely correct, but I can’t figure out where.I think it all boils down to two things I don’t know/understand:
- How to the function signatures interact, i.e. what is the $page and the $section I pass to the standard functions?
- How do I name my form input correctly?
But there may be more I’m not getting, or getting wrong.
Here’s my code:
add_action ('admin_menu', 'fd_create_settings'); add_action ('admin_init', 'fd_register_settings' ); function fd_create_settings() { add_options_page( 'Vlag van de Dag' , 'Vlag van de Dag' , 'manage_options' , 'fd_settings' , 'fd_get_settings_page' ); } function fd_get_settings_page() { echo '<div class="wrap">'; echo '<h1>Vlag van de Dag-instellingen</h1>'; echo '<form method="post" action="options.php">'; settings_fields ('fd_options'); // Is this correct? ^ do_settings_sections ('fd_options'); // Is this correct? ^ submit_button(); echo '</form>'; echo '</div>'; } function fd_register_settings() { register_setting ('fd_options', 'fd_current_flag'); // Is this correct? ^ add_settings_section ( 'fd_plugin_main_section' , 'Instellingen' , 'fd_get_text_section_main' , 'fd_options'); // Is this correct? ^ add_settings_field ( 'fd_current_flag' , 'Huidige vlag' , 'fd_get_flags_dropdown' , 'fd_options' , 'fd_plugin_main_section'); // Is fd_options correct here? ^ } function fd_get_text_section_main() { echo '<p>Algemene instellingen Vlag van de Dag.</p>'; } function fd_get_flags_dropdown() { $flags = fd_get_flags_array(); // $flags array is correctly filled with key/value pairs, I've verified // So I won't bore you with that code $options = get_option('fd_options'); // $ditto for options array $current = $options['fd_current_flag']; // $current variable fills correctly with the last saved value echo "<select id='fd_current_flag' name='fd_current_flag'>"; // What I'm nut sure of is if this is correct ^ // I thought the name should refer to the option name? // Filling the dropdown; the logic to determine the selected option // works perfectly, $current gets selected $option = '<option value="geen"'; if($current=='geen') { $option .= ' selected'; } $option .= '>Geen</option>'; echo $option; foreach($flags as $key => $flag): $option = "<option value='$key'"; if($key==$current) { $option .= ' selected'; } $option .= ">$flag</option>"; echo $option; endforeach; echo '</select>'; }
The page I need help with: [log in to see the link]
- The topic ‘I broke my plugin Options page, please help me fix it’ is closed to new replies.