Creating Settings Page with dropdowns for Plugin

It sounds like you have some of your Settings API calls wrong. Your dropdown input is created in the callback of your add_settings_field(). Each settings field needs to be told which settings section it is part of, and then your options page communicates which settings sections it needs to display through the do_settings() function.
Here’s a short code example using a checkbox (omitting a few of the callback functions, but ALL of your callbacks need to be valid functions– even if you make them empty functions):

function plugin_admin_init()
{
     //Register your plugin's options with the Settings API, this will be important when you write your options page callback, sanitizing callback omitted
     register_setting( 'plugin_options_group', 'plugin_options_name', 'sanitizing_callback' );
     //Add a new settings section, section callback omitted
     add_settings_section( 'section_id', 'Plugin Settings', 'section_callback', 'section_options_page_type' );
     //Add a new settings field
     add_settings_field( 'plugin_checkbox', 'Send automated emails?', 'field_callback', 'section_options_page_type', 'section_id' );
}
add_action('admin_init', 'plugin_admin_init');

function field_callback()
{
     //The key thing that makes it a checkbox or textbox is the input type attribute. The thing that links it to my plugin option is the name I pass it.
     echo "<input id='plugin_checkbox' name="plugin_options_name[plugin_checkbox]" type="checkbox" value="true" />"
}

function setup_menu()
{
     add_menu_page('Page Name', 'Page Name', 'user_capability_level', 'page_slug', 'page_callback');
}
add_action('admin_menu', 'setup_menu');

function page_callback()
{
?>
<div class="wrap">
     <h2>Page Name</h2>
     <form method='post' action='options.php'>
     <?php
     //Must be same name as the name you registered with register_setting()
     settings_fields('plugin_options_group');
     //Must match the type of the section you want to display
     do_settings_section('section_options_page_type');
     ?>
          <p class="submit">
               <input name="submit" type="submit" id='submit' class="button-primary" value="<?php _e("Save Changes") ?>" />
          </p>
     </form>
</div>
<?php
}
?>

Leave a Comment