Hello,
I'm setting metaboxes with [[LINK href="http://http://www.farinspace.com/wpalchemy-metabox/"]]WP Alchemy[[/LINK]]
Basically you set them like:
1. Radio button
<?php $mb->the_field('field_name'); ?>
<input type="radio" name="<?php $mb->the_name(); ?>" value="image"<?php echo $mb->is_value('image')?' checked="checked"':''; ?>/> Radio1
<input type="radio" name="<?php $mb->the_name(); ?>" value="video"<?php echo $mb->is_value('video')?' checked="checked"':''; ?>/> Radio2
2. Checkbox
<?php $mb->the_field('checkbox'); ?>
<input type="checkbox" name="<?php $mb->the_name(); ?>" value="yes"<?php $mb->the_checkbox_state('yes'); ?>/>Check to show
3. Select
<?php $mb->the_field('s_single'); ?>
<select name="<?php $mb->the_name(); ?>">
<option value="">Select...</option>
<option value="a"<?php $mb->the_select_state('a'); ?>>a</option>
<option value="b"<?php $mb->the_select_state('b'); ?>>b</option>
<option value="c"<?php $mb->the_select_state('c'); ?>>c</option>
</select>
4.Input
<input type="text" name="<?php $metabox->the_name('width'); ?>" value="<?php $metabox->the_value('width'); ?>"/>
So the question is how do you set default values for these fields?
For example, I want have first radio selected by default, checkbox selected by default, select 'b' value by default and have a text sample for input.
Does it make sense? Thanks in advance.
Jurre Hanema answers:
Try something like this:
<?php
$mb->the_field('field_name');
if(is_null($mb->get_the_value()))
$mb->meta[$mb->name] = 'some default value';
?>
<input type="text" name="<?php $metabox->the_name(); ?>" value="<?php $metabox->the_value(); ?>"/>
This won't work in all cases where WPAlchemy can be used... but I think it should work with the way how you set up your meta box templates.
Jurre Hanema comments:
@John Cotton: What does your answer have to do with the issue of setting a default value for a field in WPAlchemy? Anyway, thanks for pointing out the existense of the selected()- and checked()-functions, those will save me some ugly if-statements in the future!
denoizzed comments:
Thanks, but I would rather have examples for radio / checkbox / select since regular input fields are not so important.
Jurre Hanema comments:
Radio button:
<?php
$mb->the_field('field_name');
if(is_null($mb->get_the_value()))
$mb->meta[$mb->name] = 'video';
?>
<input type="radio" name="<?php $mb->the_name(); ?>" value="image"<?php echo $mb->is_value('image')?' checked="checked"':''; ?>/> Radio1
<input type="radio" name="<?php $mb->the_name(); ?>" value="video"<?php echo $mb->is_value('video')?' checked="checked"':''; ?>/> Radio2
Checkbox:
<?php
$mb->the_field('checkbox');
if(is_null($mb->get_the_value()))
$mb->meta[$mb->name] = 'yes';
?>
<input type="checkbox" name="<?php $mb->the_name(); ?>" value="yes"<?php $mb->the_checkbox_state('yes'); ?>/>Check to show
Select:
<?php
$mb->the_field('s_single');
if(is_null($mb->get_the_value()))
$mb->meta[$mb->name] = 'b';
?>
<select name="<?php $mb->the_name(); ?>">
<option value="">Select...</option>
<option value="a"<?php $mb->the_select_state('a'); ?>>a</option>
<option value="b"<?php $mb->the_select_state('b'); ?>>b</option>
<option value="c"<?php $mb->the_select_state('c'); ?>>c</option>
</select>
And for a multiple select with multiple default values just use an array:
if(is_null($mb->get_the_value()))
$mb->meta[$mb->name] = array('b', 'c');
denoizzed comments:
Thanks, works perfectly.
John Cotton answers:
You need to use [[LINK href="http://codex.wordpress.org/Function_Reference/checked"]]WordPress's checked function[[/LINK]]:
<input type="radio" name="<?php $mb->the_name(); ?>" value="image" <?php echo checked( $mb->get_the_value($mb->get_the_name()), 'image'); ?>/> Radio1
<input type="radio" name="<?php $mb->the_name(); ?>" value="video" <?php echo checked( $mb->get_the_value($mb->get_the_name()), 'video'); ?>/> Radio2
It returns the correct checked html if the two values passed match (so order doesn't matter).
There's a selected function too!
denoizzed comments:
I don't need to handle the checking / selecting, it's already handled by the class. I need to set first as checked by default.
Just Me answers:
you could store the settings in an options array and change the creation of the metabox using the shorthand if loop. If no value has been set, use the default (the ones in the options array).
I am using it like this in one of my plugins (different structure but the idea is the same).
$my_options = get_options('my_options'); //read values from database
$my_thumbnail = ($my_options['thumbnail'] == '')? 'N':$my_options['thumbnail'];
creating the form (in your case metabox):
<input type="radio" name="my_thumbnail" value="N" <?php echo ($my_thumbnail == "N") ? 'checked' : ''; ?> /><?php _e( 'No', 'my_language_file');?>
<input type="radio" name="my_thumbnail" value="Y" <?php echo ($my_thumbnail == "Y") ? 'checked' : ''; ?> /><?php _e( 'Yes', 'my_language_file'); ?> <br />