6

What is the best way to go about setting up default values for a plugin? Should I just insert those values into the wp_options table?

AND...if that's the best way to go about it, I have another question. My options are listed as a group which currently looks like:

a:4:{s:26:"nc_location_drop_down_zoom";s:2:"14";s:17:"nc_location_width";s:3:"200";s:29:"nc_location_drop_down_maptype";s:7:"roadmap";s:11:"text_string";s:0:"";}

Is this a serialized array? How do I do an insert like this into the table? (I realized this is more of an sql question...)

  • 1
    FYI: WordPress functions deal with serialization for you, depending on where you've stored that data will depend what function you need, but assuming an option, calling get_option will be sufficient in unserializing the data. – t31os Aug 16 '11 at 23:09
  • you can do like they say here wordpress.org/support/topic/… – user10879 Dec 6 '11 at 0:48
1

Use the Settings API and save your data in a single option as an array, WordPress will serialize the data for you.

  • I'm using the Settings API to set up my options...but when I try to set the default in my callbacks it just overrides the user choices...is there a specific method in the API that is used to set the defautls. – redconservatory Aug 16 '11 at 23:22
  • 1
    you just need to check if the option already exists, and only write the defaults if it doesn't. this is typically done in a register activation hook. – Milo Aug 16 '11 at 23:32
  • Hi @redconservatory I have posted an answer which works for me maybe you could take a look? – Remzi Cavdar Nov 5 '18 at 9:01
14

You should do defaults at the time of pulling the data out. Never insert default values into the database. Defaults are default. Options in the DB override defaults.

How to do defaults for a serialized options array:

$defaults = array(
  'default1' => '1',
  'default2' => '2',
);
$options = wp_parse_args(get_option('plugin_options'), $defaults);
  • 1
    And when the array is more complex, will wp_parse_args deal with the data correctly? ie. in cases where you have arrays of arrays and so forth. – t31os Aug 17 '11 at 9:00
  • It will only be looking at the top level of the array and whether the keys exist or not. If you're nesting arrays for options, you may be making it overly complicated. – Otto Aug 17 '11 at 16:00
  • 2
    I don't know about being overly complicated, it's quite reasonable that i may choose to store other data with those option names and values, as is done with many theme options, for example you may store info about the type of option(checkbox, text, etc), a description and perhaps more. I guess you could always create a secondary relational array to hold that data though(but i think that itself might be overly complicating the task). – t31os Aug 17 '11 at 16:07
1

In addition to the answer by Otto.

If you have the multidimensional options array and you still want it to merge with the array of defaults, use the following function in place of wp_parse_args():

<?php
function meks_wp_parse_args( &$a, $b ) {
    $a = (array) $a;
    $b = (array) $b;
    $result = $b;
    foreach ( $a as $k => &$v ) {
        if ( is_array( $v ) && isset( $result[ $k ] ) ) {
            $result[ $k ] = meks_wp_parse_args( $v, $result[ $k ] );
        } else {
            $result[ $k ] = $v;
        }
    }
    return $result;
}

For example,

<?php
$defaults = array(
    'setting-1' => array(
        'option-1' => 1,
        'option-2' => 0,
    ),
    'setting-2' => 1
);

// Only variables are passed to the function by reference (Strict Standards warning)
$options = get_option('plugin_options');
$options = meks_wp_parse_args($options, $defaults);

The recursive function was found here.

1

Use add_option. If you use add_option existing options will not be updated and checks are performed to ensure that you aren’t adding a protected WordPress option.

See add_option at developer.wordpress.org

// Activation
function name_plugin_activation(){
    do_action( 'name_plugin_default_options' );
}
register_activation_hook( __FILE__, 'name_plugin_activation' );


// Set default values here
function name_plugin_default_values(){

    // Form settings
    add_option('name_form_to', 'info@example.com');
    add_option('name_form_subject', 'New');


}
add_action( 'name_plugin_default_options', 'name_plugin_default_values' );

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.