I am still learning settings API so please forgive my noob question. I have three fields: a checkbox, colorpicker and text field (they are working), but I would like to do this: when the theme is installed add "checked" state to my checkbox but allow user to "unchecked" it ,and if empty add default color to my colorpicker... I have no idea how to do this so please help me!

My code looks like this now (thanks to @Chip Bennett! ) :

function create_theme_options_page(){
add_options_page('Theme Options','Theme Options','administrator',__FILE__,'build_options_page');
   function build_options_page(){
   <div id="theme-options-wrap">
   <div style="float:left;margin-top:19px;position:absolute;" class="icon32" id="icon-tools"><br/></div>
   <h2>My options page</h2>
   <form method="post" action="options.php">
   <?php settings_fields('plugin_options');?>
   <?php do_settings_sections(__FILE__); ?>
   <p class="submit">
   <input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save changes');?>"/>

     function register_and_build_fields(){
     add_settings_section('main_section','<br/><br/>Main Settings','section_cb',__FILE__);
     add_settings_field('banner_heading','Banner Heading:','banner_heading_setting',__FILE__,'main_section');
     add_settings_field('display_sidebar','Display Sidebar:','display_sidebar_setting',__FILE__,'main_section');
     add_settings_field('display_colorpicker','Display Colorpicker:','display_colorpicker_setting',__FILE__,'main_section');
       //Install Iris colorpicker
       add_action( 'admin_enqueue_scripts', 'mw_enqueue_color_picker' );
       function mw_enqueue_color_picker( $hook_suffix ) {
       // first check that $hook_suffix is appropriate for your admin page
       wp_enqueue_style( 'wp-color-picker' );
       wp_enqueue_script( 'my-script-handle', get_template_directory_uri() . '/js/my-script.js', array( 'wp-color-picker' ), false, true );

         //Now how to add a default color on theme install?
         function display_colorpicker_setting(){
         $options = get_option('plugin_options');
         echo"<input type='text' id='color1' name='plugin_options[display_colorpicker]' value='{$options['display_colorpicker']}'/>";

          //Sidebar checkbox
          //Now how to add a default state "checked" on theme install?
          function display_sidebar_setting(){
          $options = get_option('plugin_options');
          echo "<input type='checkbox' name='plugin_options[display_sidebar]' {$options['display_sidebar']} id='display_sidebar' />";

            //Banner Heading
            function banner_heading_setting(){
            $options = get_option('plugin_options');
            echo "<input name='plugin_options[banner_heading]' type='text' value='{$options['banner_heading']}'/>";
              //Validate our settings here
              function validate_setting($plugin_options){   
              return $plugin_options;

               function section_cb(){
  • 3
    Please, don't post "flat" code. Use indentation to format your code. No one will read that. – kaiser Feb 23 '13 at 17:39
  • Hope it is better now? – Dejo Dekic Feb 23 '13 at 17:48
  • Ehm... Thanks for your effort, but you should really read about this somewhere. :) – kaiser Feb 23 '13 at 23:47
  • @DejoDekic, see codex.wordpress.org/WordPress_Coding_Standards – Fred Dec 12 '14 at 14:00

You can pass defaults to get_option. The second parameter is a fallback.

$default (mixed) (optional) The default value to return if no value is returned (ie. the option is not in the database).

    Default: false

So, for example, instead of:

  $options = get_option('plugin_options');

You'd want:

  $options = get_option('plugin_options',array('display_sidebar' => true));

Or you can insert your default options with add_option, which is "A safe way of adding a named option/value pair to the options database table. It does nothing if the option already exists."

I'd do the latter. It should be easier.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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