$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>options - How to set up default values for a plugin?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

options - How to set up default values for a plugin?

matteradmin9PV0评论

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...)

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...)

Share Improve this question asked Aug 16, 2011 at 22:35 redconservatoryredconservatory 2,5097 gold badges28 silver badges43 bronze badges 2
  • 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 Commented Aug 16, 2011 at 23:09
  • you can do like they say here wordpress/support/topic/… – user10879 Commented Dec 6, 2011 at 0:48
Add a comment  | 

4 Answers 4

Reset to default 2

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

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);

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.

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

// 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', '[email protected]');
    add_option('name_form_subject', 'New');


}
add_action( 'name_plugin_default_options', 'name_plugin_default_values' );
Post a comment

comment list (0)

  1. No comments so far