1

I have to create a option page in my custom theme wordpress. here is is a sample of code

<?php

    function bguru_register_settings(){

   $default_options=array(
        'bguru_logo'=>'http://templategraphy.com/demo/businessguru/images/logo.png',
        'bguru_vimeo'=>'',
        'bguru_skype'=>'',
        'bguru_dribbble'=>'',
        'bguru_slide_one_image'=>'',
        'bguru_slide_one_heading'=>'',
        'bguru_slide_one_text'=>''
         );

        add_option('bguru_options',$default_options);

        register_setting('tgbusinessguru', 'bguru_options');
    }

    add_action('admin_init', 'bguru_register_settings');

    function bguru_register_options_page(){

    add_theme_page('Business Guru Options', 'Theme Customizer', 'edit_theme_options', 'bguru-options', 'bguru_options_page');

}

    add_action('admin_menu', 'bguru_register_options_page');

    function bguru_options_page(){
        $options=get_option('bguru_options');

        ?>
<div class="wrap">
    <?php
 screen_icon(); ?>
    <h1>Business Guru Options</h1>
    <form method="post" action="options.php"> 
        <?php
 settings_fields('tgbusinessguru'); ?>
            <table class="form-table">
                <tr valign="top">
                    <th><h2>General</h2><th>
                </tr>
                <tr valign="top">

                    <th scope="row"><label for="bguru_logo">Logo:</label></th>

                    <td><input type="text" id="bguru_logo" size="50" name="bguru_options[bguru_logo]" value="<?php echo $options['bguru_logo']; ?>" /></td>

                </tr>

                <tr valign="top">
                    <th><h2>Social Links</h2><th>
                </tr>
                <tr valign="top">
                    <th scope="row"><label for="bguru_vimeo">Vimeo:</label></th>
                    <td><input type="text" id="bguru_vimeo" size="50" name="bguru_options[bguru_vimeo]" value="<?php echo $options['bguru_vimeo']; ?>" /></td>

                 </tr>
                <tr valign="top">
                    <th scope="row"><label for="bguru_skype">Skype:</label></th>
                    <td><input type="text" id="bguru_skype" size="50" name="bguru_options[bguru_skype]" value="<?php echo $options['bguru_skype']; ?>" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row"><label for="bguru_dribbble">Dribbble:</label></th>
                    <td><input type="text" id="bguru_dribbble" size="50" name="bguru_options[bguru_dribbble]" value="<?php echo $options['bguru_dribbble']; ?>" /></td>
                </tr>
                <tr valign="top">

                    <th scope="row"><label for="bguru_slide_one_image">Image:</label></th>

                    <td><input type="text" id="bguru_slide_one_image" size="50" name="bguru_options[bguru_slide_one_image]" value="<?php
 echo $options['bguru_slide_one_image']; ?>" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row"><label for="bguru_slide_one_heading">Heading:</label></th>
                    <td><input type="text" id="bguru_slide_one_heading" size="50" name="bguru_options[bguru_slide_one_heading]" value="<?php
 echo $options['bguru_slide_one_heading']; ?>" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row"><label for="bguru_slide_one_text">Description:</label></th>
                    <td><textarea type="text" id="bguru_slide_one_text" style="width:439px;height:100px;" name="bguru_options[bguru_slide_one_text]"><?php  echo $options['bguru_slide_one_text']; ?></textarea></td>
                </tr>
              </table>
          <?php submit_button(); ?>
       </form>
     </div>
<?php  } ?>

All the form details are passed by dashboard I want if admin forget to fill the logo field then by default this $default_options work so by default logo field fill up but when I try to access the array using get_option I'm not getting the results I expect.

  • Please suggest some solution what i am doing wrong your suggestions really help me – Payal Jul 14 '14 at 11:03
  • I edit my code please do check my edited code and suggest some solutions – Payal Jul 15 '14 at 9:16
4

I think that you have added bguru_options option before. If bguru_options already exists, add_option() does nothing. To modify the value of existing options you should use update_option() instead.

EDIT

I confirm what I thought. You are running add_option('bguru_options', $default_options); in every admin_init. bguru_options option was added to database in the first run and subsequent calls to add_option('bguru_options', $default_options); are doing nothing.

  • Use update_option() to change the value of a existent option. If the option doesn't exist, it will be created.
  • Use add_option() if you really need it. For example, if you need to set autoload=no. This parameter is not accepted by update_option(). Do this preferably during plugin/theme activation
  • If you need to use add_option() and you are not sure if the option already exists, use delete_option() before call add_option().

EDIT 2

I've tested your code exactly as it is and I've tested it. The default logo URL is correctly added to database in the bguru_options option and returned by get_option('bguru_options');:

$options=get_option('bguru_options');
var_dump($options['bguru_logo']);

There is no problem at all. The only drawback is that if the you leave the URL field empty, the default logo URL is not set because, I repeat, add_option() is doing nothing in subsequent calls because bguru_options option already exists.

I've tested the code and it works. If it does not work for you I will need more information. Maybe some debug log?

Anyway, I wnat to show you how get_option() support default values without the need of store default values in the database:

$default_options=array(
     'bguru_logo'=>'http://templategraphy.com/demo/businessguru/images/logo.png',
     'bguru_vimeo'=>'',
     'bguru_skype'=>'',
     'bguru_dribbble'=>'',
     'bguru_slide_one_image'=>'',
     'bguru_slide_one_heading'=>'',
     'bguru_slide_one_text'=>''
 );
 $bguru_options = get_option('bguru_options', $bguru_options_defaults);
 //Set defaults for unsaved subset in the array
 $bguru_options = wp_parse_args( $bguru_options, $bguru_options_defaults );
  • Thanks @cybmeta for reply no i am not added any bguru_option before i also used update_option() but the issue remain the same.please suggest some solution.. – Payal Jul 14 '14 at 10:46
  • Where/When are you executing bguru_register_settings()? – cybmeta Jul 14 '14 at 11:38
  • Again thanks @cybmeta for your reply below register_setting() check my updated code – Payal Jul 14 '14 at 11:41
  • I've edited the answer. – cybmeta Jul 14 '14 at 14:58
  • Thanks you so much @cybmeta for your reply i am not sure bguru_option already exits or not so i used delete_option() before add_option() as you said delete_option('bguru_options'); add_option('bguru_options',$default_options); still the problem remain the same. – Payal Jul 15 '14 at 6:00
-1

You should encode the array to json or serialize it before adding it as an option:

  add_option('bguru_options',json_encode($default_options));

or

  add_option('bguru_options',serialize($default_options));
  • Thanks @lucian for your reply i am doing the same as you said but my problem remain the same.. – Payal Jul 15 '14 at 5:42

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.