admin管理员组文章数量:1130349
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 |4 Answers
Reset to default 2Use 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' );
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_optionwill 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
4 Answers
Reset to default 2Use 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' );
本文标签: optionsHow to set up default values for a plugin
版权声明:本文标题:options - How to set up default values for a plugin? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749218936a2334634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


get_optionwill be sufficient in unserializing the data. – t31os Commented Aug 16, 2011 at 23:09