I have multiple radio buttons set up inside of a metabox in a custom post type. I have created the metabox like so:
add_action('admin_init', 'add_meta_boxes', 1);
function add_meta_boxes() {
add_meta_box( 'repeatable_fields', 'Top 10 Movie List', 'repeatable_meta_box_display', 'cpt_top_ten_list', 'normal', 'default');
}
function repeatable_meta_box_display() {
global $post;
$repeatable_fields = get_post_meta($post->ID, 'repeatable_fields', true);
wp_nonce_field( 'repeatable_meta_box_nonce', 'repeatable_meta_box_nonce' );
if ( $repeatable_fields ) :
// set a variable so we can append it to each row
$num = 0;
$second_num = 0;
foreach ( $repeatable_fields as $field ) {
$num++;
<div class=" playbackformat-holder-<?php echo $num; ?> playbackformat-holder">
<form id="playback-form-<?php echo $num; ?>">
<label for="dvd-<?php echo $num; ?>">
<input type="radio" class="playbackformat-holder-radiobutton" value="dvd" name="playback_format[<?php echo $second_num; ?>]" id="dvd-<?php echo $num; ?>" <?php if($field['playback_format'] == 'dvd') { echo 'checked'; } ?>>DVD
</label>
<label for="bluray-<?php echo $num; ?>">
<input type="radio" class="playbackformat-holder-radiobutton" value="bluray" name="playback_format[<?php echo $second_num; ?>]" id="bluray-<?php echo $num; ?>" <?php if($field['playback_format'] == 'bluray') { echo 'checked'; } ?>>Bluray
</label><br>
<label for="3d-<?php echo $num; ?>">
<input type="radio" class="playbackformat-holder-radiobutton" value="3d" name="playback_format[<?php echo $second_num; ?>]" id="3d-<?php echo $num; ?>" <?php if($field['playback_format'] == '3d') { echo 'checked'; } ?>>3d
</label><br />
</form>
</div>
second_num++;
}
And here is my save function to store the data into an array.
add_action('save_post', 'repeatable_meta_box_save', 10, 2);
function repeatable_meta_box_save($post_id) {
if ( ! isset( $_POST['repeatable_meta_box_nonce'] ) ||
!wp_verify_nonce( $_POST['repeatable_meta_box_nonce'], 'repeatable_meta_box_nonce' ) )
return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if (!current_user_can('edit_post', $post_id))
return;
$old = get_post_meta($post_id, 'repeatable_fields', true);
$new = array();
$playbackFormats = $_POST['playback_format'];
$count = count( $names ) - 1;
for ( $i = 0; $i < $count; $i++ ) {
// currently only storing the last stored radio value
$new[$i]['playback_format'] = $playbackFormats[$i];
// save movie description
// and however you want to sanitize
if ( !empty( $new ) && $new != $old ) {
update_post_meta( $post_id, 'repeatable_fields', $new );
} elseif ( empty($new) ) {
delete_post_meta( $post_id, 'repeatable_fields', $old );
}
}
The problem I am facing is that , lets say I have three rows. If I set the first to DVD, the second to bluray and the third to 3D only the last value stores as 3D. All other ['playback_format']
store as ‘null’.
The way I have things set up it looks like things should be saving properly. This is a trimmed down version. I have input fields that are storing correctly, but the radio buttons have been a major issue.
It looks like my save function should be storing each ['playback_format']
into its appropriate place in the array. But only one stores. I’ve gotten it to store the same value for all three rows, but can’t get a unique value for each row and not sure why.
Any help would be appreciated, as this is the last piece of the puzzle!
Thanks! (Screenshot below for a visual aid)
Here is the value of var_dump($field);
after storing the data in the screenshot above.
array(4) {
["name"]=> string(116) "http://onecinephile.staging.wpengine.com/wp-content/uploads/2014/01/lon-chaney-phantom-hunchback-penalty-300x280.jpg"
["select"]=> string(14) "Test #2"
["url"]=> string(35) "Testing movie description #2"
["playback_format"]=> string(3) "dvd"
}
array(4) {
["name"]=> string(103) "http://onecinephile.staging.wpengine.com/wp-content/uploads/2014/01/spencer-tracy-boys-town-300x225.jpg"
["select"]=> string(14) "Test #1"
["url"]=> string(35) "Testing movie description #1"
["playback_format"]=> NULL
}
array(4) {
["name"]=> string(100) "http://onecinephile.staging.wpengine.com/wp-content/uploads/2014/01/Ronald-and-Madeleine-225x300.jpg" ["select"]=> string(14) "Test #3
["url"]=> string(35) "Testing movie description #3"
["playback_format"]=> NULL
}
So I came back to this after practice and implemented some of the Alchemy setup, meaning all the inputs have names like
name="_movies[3][title]"
where 3 is the iteration we’re on in the loop.Create the Metabox
Save the Meta
Saving is now relatively easy because the arrays in the names sets it up for us:
I will say that I ran into the same issue that we talked about in the chat…. I about flipped when after all this I was still running into the second and third ‘playback_format’ radios weren’t evening posting. But finally, I noticed that you are wrapping the radios in a
<form>
element. I’m convinced this is the problem because as soon as I got rid of it, it worked.I would suggest you try dropping that extra
<Form>
from your code first to see if that solves it. It might be the rest of your code works. If not, then I’m pretty sure mine does now… however, I’ve changed some names and some IDs so that will probably effect the rest of the work that relies on this.And finally, b/c of how I changed everything, I needed to modify your add-row JS. It is simplified here as I didn’t want to deal with the tinyMCE editors, so I just left the textareas for my testing purposes.
Duplicate Row with Javascript