Creating Settings Page with dropdowns for Plugin

I am setting up my plugin’s settings page and I am able to get the fields I want on the page, but I want them to be dropdowns not textboxes… I have not found any useful info on the web on how to do this other than using add_settings_field. With add_settings_field I can display a dropdown for my setting but I can’t get it to appear on my plugin settings page, I can get it to appear on the ‘reading settings page’ though for example.

basically I have used

Read More

add_options_page() so that my Plugin Options page shows up under the settings menu in the wordpress backend, and addaction() to register my settings, and the form is built with the callback function from add_options_page. This all works, I just can’t get anything but textboxes… and I would like dropdowns and probably checkboxes.

I hope that makes sense.

Thanks!

Related posts

Leave a Reply

1 comment

  1. 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
    }
    ?>