Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Set default options for plugin on activation and blog creation WordPress

Hello,

I would like to add default setting to my plugin.
The thing is I'm using settings_fields() to save the options in database and the option is in an array even if I
have only one option.

I tried to use this code:
<blockquote>
//default options


function default_option() {
update_option ('mbgalerie_sample','1');
}
register_activation_hook(__FILE__,'default_option');
</blockquote>

but it's not working. It's for a multiuser setup. I want that when a user registers the default option is added in the database
i.e in wp_'blog_id'_options where the options are saved


<blockquote>

add_action('admin_init', 'galerie_init' );
function galerie_init(){
register_setting( 'galerie_options', 'mbgalerie_sample', 'galerie_validate' );
}


//default options


function default_option() {
update_option ('mbgalerie_sample','1');
}
register_activation_hook(__FILE__,'default_option');



<div class="wrap">

<h2 class="titre">Options</h2>

<form method="post" action="options.php">

<?php

settings_fields('galerie_options');
$op = get_option('mbgalerie_sample');
$galerytype=$op['galerytype'];

if (isset ($_GET['settings-updated']) AND $_GET['settings-updated']=='true')
{
?>


<div class="updated fade" id="message"><p>options changed !</p></div>
<?php
}
?>
<table class="form-table">


<tr valign="top">

<th scope="row" class="titre2">Choose :</th>

<td>
<input type="radio" name="mbgalerie_sample[galerytype]" value="1" id="1"<? if ($galerytype=="1") {echo 'checked="checked"';} ?> /> <label for="1">Normal</label><br />
<input type="radio" name="mbgalerie_sample[galerytype]" value="2" id="2"<? if ($galerytype=="2") {echo 'checked="checked"';} ?> /> <label for="2">Miniatures</label><br />
</td>
</tr>

</table>

<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>

</form>

</blockquote>

Thanks all

Answers (2)

2011-07-11

Pippin Williamson answers:

Instead of setting a default option, you should check for whether the option is set on front end. If no option is set, use a default value. It is better not to populate the database with default options.

You can check if an option is set like this:


$options = get_option('my_site_settings');
if($option['my_option_name']) {
// the option is set, so use the user-set value
} else {
// the option is no set, so use a default value here
}

2011-07-11

Dylan Kuhn answers:

To me it looks like you want to add the option when a new blog is created. Adding a an example prefix of eg_, maybe something like this:



function eg_default_option($blog_id, $user_id, $domain, $path, $site_id, $meta) {
update_blog_option($blog_id,'mbgalerie_sample','1');
}
add_action( 'wpmu_new_blog', 'eg_default_option', 10, 6);