Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In the attached screenshot you can see the structure of the theme options we are working with. There are 8 tabs and for each tab, we have created custom fields using code similar to what can be seen below.This is how we are setting the default values for each field. ( ie. default = 'http://facebook.com/samplefacebooktitle')

function facebook_callback() {

    $item ='facebook';
    $default ='http://facebook.com/samplefacebooktitle';
    $group = 'general_display_options';
    $input = create_input($item,$default,$group);
    echo $input;
}

and then the create_input function:

function create_input($item,$default,$group) {

    $obj = $group.'['.$item.']';
    $options = get_option($group); 
    if ($options[$item] != '') { 
        $value= $options[$item]; 
    } else { 
        $value = $default; 
    }
    return '<input type="text" id="'.$item.'" name="'.$obj.'" value="'. $value .'" />';

}

In order to initialize the defaults though, we need to save each tab individually the first time the theme loads and this theme is to be customized for internationalization by many companies in other countries. So, the question is, Is there a simple way to pre-load these default values and have them saved automatically before the theme options are saved the first time. And if not, does anyone know a way to save all theme options at once when you click save on any given tab?

------- EDIT ---------------------

Thanks to Chip Bennett for helping me get this working! The below code is a bit gnarly, but it works, and sets up the defaults on theme activation. Also added is a check to see if values are '' and if so, we re-set them to the defaults created in the create_general_defaults() function. Please feel free to let me know ways to optimize this code.

<?php

function theme_menu() {
    add_theme_page(
        'My Theme',
        'My Theme',
        'administrator',
        'theme_options',
        'theme_display'
    );

} 
add_action('admin_menu', 'theme_menu');

function theme_display( $active_tab = '') {
?>

    <div class="wrap">
        <!-- add icon to the page -->
        <div id="icon-themes" class="icon32"></div>
        <h2>Theme Options</h2>
        <?php settings_errors(); ?>
        <?php
            if(isset($_GET['tab'])) {
                $active_tab = $_GET['tab'];
            } else if ($active_tab == 'nav_options') { 
                $active_tab = 'nav_options';
            } else {
                $active_tab = 'display_options';
            }
        ?>
        <h2 class="nav-tab-wrapper">
            <a href="?page=theme_options&tab=display_options" class="nav-tab <?php echo $active_tab == 'display_options' ? 'nav-tab-active' : ''; ?>">General</a>
            <a href="?page=theme_options&tab=nav_options" class="nav-tab <?php echo $active_tab == 'nav_options' ? 'nav-tab-active' : ''; ?>">Nav</a>
        </h2>
        <form method="post" action="options.php">
                <?php
                    if( $active_tab == 'display_options' ) {
                        settings_fields( 'theme_display_options' );
                        do_settings_sections( 'theme_display_options' );
                    } else if( $active_tab == 'nav_options' ) {
                        settings_fields( 'theme_nav_options' );
                        do_settings_sections( 'theme_nav_options' );
                    }
                    submit_button();
                ?>
        </form>

    </div>
<?php
}

/* -------------------- * 
 *  register settings
 *  -------------------- */
//global $theme_display_get_options;
// $theme_display_get_options = theme_display_get_options();

function initialize_theme_options() {

    // verify if the options exist
    if(false == get_option('theme_display_options')) {
        add_option('theme_display_options');
    }

    add_settings_section(
        'display_settings_section','General Options','display_options_callback','theme_display_options'
    );

    add_settings_field('facebook_alt','Facebook Alt Tag','facebook_alt_callback','theme_display_options','display_settings_section');

    register_setting('theme_display_options','theme_display_options');

}
add_action('admin_init','initialize_theme_options');


function display_options_callback() {
    echo '<p>Enter the necessary values for the following fields.</p>';
}

function create_general_defaults() {
    global $general_defaults;
    $general_defaults = array(
        'theme_title' => 'My Title Yo',
        'facebook' => 'http://facebook.com',
        'facebook_alt' => 'My default'
    );

    //save these in case something is blank and it will return to its default value
    $general_orig_defaults = apply_filters('my_options_defaults', $general_defaults);
    return $general_orig_defaults;
}
 create_general_defaults();

function get_general_defaults() {
    $option_defaults = create_general_defaults();
    global $the_theme_options;
    $the_theme_options = wp_parse_args( get_option('theme_display_options', array() ), $option_defaults );
    return $the_theme_options;
}
get_general_defaults();

function facebook_alt_callback() {

    global $general_defaults;
    global $orig_defaults;

    $orig_defaults = $general_defaults;
    $item ='facebook_alt';
    $group = 'theme_display_options';
    $form_object = 'text';
    callback_standard($item, $group, $form_object, $orig_defaults);

}

function callback_standard($item, $group, $form_object) {
    global $the_theme_options;
    global $orig_defaults; // defaults set in the 

    $default = $the_theme_options[$item];
    if($default == '') {
        $default = $orig_defaults[$item];
    }

    $obj = $group.'['.$item.']';
    if ($form_object == 'text') {
        echo '<input type="text" id="'.$item.'" name="'.$obj.'" value="'. $default .'" style="width:300px;" />';
    } else if ($form_object == 'textarea') {
        '<textarea id="'.$item.'" name="'.$obj.'" rows="5" style="width:500px" />'. $value .'</textarea>';
    }
}


/*  --------------------------------------------------------
*   Navigation Options
*   --------------------------------------------------------*
*/

// left off for abbreviated example


/*  ----------------------------------------------------
*   Templated settings field output
*   ----------------------------------------------------
*/
function create_input($item,$default,$group) {

    $obj = $group.'['.$item.']';
    $options = get_option($group);
    if ($options[$item] != '') {
        $value= $options[$item];
    } else {

        $value = $default;
    }
    return '<input type="text" id="'.$item.'" name="'.$obj.'" value="'. $value .'" style="width:300px;" />';

}
function create_textarea($item,$default,$group) {

    $obj = $group.'['.$item.']';
    $options = get_option($group);
    if ($options[$item] != '') {
        $value = $options[$item];
    } else {
        $value = $default; 
    }
    $value = stripslashes($value);
    return '<textarea id="'.$item.'" name="'.$obj.'" rows="5" style="width:500px" />'. $value .'</textarea>';

}


?>
share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

Don't initialize defaults, or do an initial save. Just use sane defaults.

For example, define an array of defaults:

function wpse92323_get_option_defaults() {
    $defaults = array(
        'slug1' => 'default1',
        'slug2' => 'default2'
    );
    return apply_filters( 'wpse92323_option_defaults', $defaults );
}

Then, write a function to return an option value, that parses stored options against your defaults:

function wpse92323_get_options() {

    // Get the option defaults
    $option_defaults = wpse92323_get_option_defaults();
    // Globalize the variable that holds the Theme options
    global $wpse92323_options;
    // Parse the stored options with the defaults
    $wpse92323_options = wp_parse_args( get_option( 'theme_wpse92323_options', array() ), $option_defaults );
    // Return the parsed array
    return $wpse92323_options;
}

Then, everywhere you use your options:

// Get options
global $wpse92323_options;
$wpse92323_options = wpse92323_get_options();

// Echo an option
echo $wpse92323_options['slug1']

Explanation of wpse92323_get_options():

  1. wp_parse_args( $args, $defaults ): will parse two arrays, $args, and $defaults, with any keys in $args overriding the same keys in $defaults
  2. get_option( 'theme_wpse92323_options', array() ): returns the value of a stored option. (Replace 'theme_wpse92323_options' with whatever you call in register_setting().) The second parameter is the default value; passing array() ensures that, if the option hasn't yet been set, an empty array is returned. (This is critical for the use of wp_parse_args().)

So, what this construct does:

  1. Get the current Theme options array
  2. If it isn't yet set, return an empty array
  3. Parse the current Theme options array against the option defaults, letting the user-defined Theme options take precedence
  4. Return the resulting, merged array

Using this method, the user or Theme never actually has to save any Theme option values to the database in order for the Theme to work properly. The Theme works properly, with defined, sane defaults, "out of the box".

share|improve this answer
    
ok, trying to implement this and I think it makes sense. I'm a little confused though. On returning their values, -- // Get options global $wpse92323_options; $wpse92323_options = wpse92323_get_options(); -- Why do I need to declare that $wpse92323_options is global a second time? –  Starfs Mar 26 '13 at 14:59
    
You declare it as a global when you define it, and then you declare it as a global when you use it. You don't actually have to do that; it just makes life easier. Otherwise, you have to call $wpse92323_options = wpse92323_get_options() anywhere and everywhere you want to use an option value. –  Chip Bennett Mar 26 '13 at 15:33
    
Thanks for the help! I'm close but not sure how to integrate this with displaying and submitting the options with my current code. So far, just getting blank defaults on my theme options page. Do you have a full-working example I could take a look at? –  Starfs Mar 26 '13 at 16:08
    
I can't write "full working example" code for your own custom code. :) If you can add some example code for your settings page to your question, I can help. –  Chip Bennett Mar 26 '13 at 18:20
1  
Thanks! got it working. The issue is that wasn't declaring the global variable within the callback function. –  Starfs Mar 27 '13 at 19:29
show 2 more comments

You should initialize the default values when the theme is first activated by hooking into the after_setup_theme action.

<?php add_action( 'after_setup_theme', 'function_name' ); ?>

Edit: Chip pointed out that this hook is actually called every page load. Make sure that if you hook this to check if you've set the defaults already - or you'll continuously overwrite them. Better yet: use Chip's answer above.

share|improve this answer
    
Actually, you shouldn't use after_setup_theme for assigning default option values. That hook is fired on every WordPress execution cycle. So, if you write option defaults at this hook, you'll just end up overwriting user settings with default settings on every execution cycle. –  Chip Bennett Mar 26 '13 at 18:23
    
Thanks for the heads up, Chip. You're absolutely right. I guess what I should suggest is that you register an option of "my_theme_has_been_installed" and if it hasn't been set then do the init and set it. I rather like your more detailed answer, though. :) –  infamouse Mar 26 '13 at 22:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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