C’est un code que j’adapte https://Gist.github.com/2057532 . Le problème est que je ne parviens pas à enregistrer le contenu des nouveaux champs que j'ai créés. J'avais vérifié les autres questions ici, mais aucune ne m'a aidé. Toute idée où est le problème?
add_action('admin_init', 'slider_metabox_caption', 1);
function slider_metabox_caption() {
add_meta_box( 'repeatable-fields', 'Captions', 'repeatable_metabox_display', 'slide', 'normal', 'high'); }
function repeatable_metabox_display() {
global $post;
$repeatable_fields = get_post_meta($post->ID, 'repeatable_fields', true);
wp_nonce_field( 'repeatable_meta_box_nonce', 'repeatable_meta_box_nonce' ); ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.metabox_submit').click(function(e) {
e.preventDefault();
$('#publish').click();
});
$('#add-row').on('click', function() {
var row = $('.empty-row.screen-reader-text').clone(true);
row.removeClass('empty-row screen-reader-text');
row.insertBefore('#repeatable-fieldset-one tbody>tr:last');
return false;
});
$('.remove-row').on('click', function() {
$(this).parents('tr').remove();
return false;
});
$('#repeatable-fieldset-one tbody').sortable({
opacity: 0.6,
revert: true,
cursor: 'move',
handle: '.sort'
});
});
</script>
<table id="repeatable-fieldset-one" width="100%">
<thead>
<tr>
<th width="1%"></th>
<th width="15%">Text</th>
<th width="4%">Fx</th>
<th width="4%">Easing</th>
<th width="4%">Speed</th>
<th width="4%">X</th>
<th width="4%">Y</th>
<th width="4%">Width</th>
<th width="4%">Height</th>
<th width="1%"></th>
</tr>
</thead>
<tbody>
<?php
if ( $repeatable_fields ) :
foreach ( $repeatable_fields as $field ) { ?>
<tr>
<td><a class="button remove-row" href="#">-</a></td>
<td><input type="text" class="widefat" name="text[]" value="<?php if($field['text'] != '') echo esc_attr( $field['text'] ); ?>" /></td>
<td><input type="text" class="widefat" name="fx[]" value="<?php if($field['fx'] != '') echo esc_attr( $field['fx'] ); ?>" /></td>
<td><input type="text" class="widefat" name="easing[]" value="<?php if($field['easing'] != '') echo esc_attr( $field['easing'] ); ?>" /></td>
<td><input type="text" class="widefat" name="speed[]" value="<?php if($field['speed'] != '') echo esc_attr( $field['speed'] ); ?>" /></td>
<td><input type="text" class="widefat" name="x[]" value="<?php if($field['x'] != '') echo esc_attr( $field['x'] ); ?>" /></td>
<td><input type="text" class="widefat" name="y[]" value="<?php if($field['y'] != '') echo esc_attr( $field['y'] ); ?>" /></td>
<td><input type="text" class="widefat" name="width[]" value="<?php if($field['width'] != '') echo esc_attr( $field['width'] ); ?>" /></td>
<td><input type="text" class="widefat" name="height[]" value="<?php if($field['height'] != '') echo esc_attr( $field['height'] ); ?>" /></td>
<td><a class="sort">|||</a></td>
</tr>
<?php } else :
// show a blank one ?>
<tr>
<td><a class="button remove-row" href="#">-</a></td>
<td><input type="text" class="widefat" name="text[]" /></td>
<td><input type="text" class="widefat" name="fx[]" /></td>
<td><input type="text" class="widefat" name="easing[]" /></td>
<td><input type="text" class="widefat" name="speed[]" /></td>
<td><input type="text" class="widefat" name="x[]" /></td>
<td><input type="text" class="widefat" name="y[]" /></td>
<td><input type="text" class="widefat" name="width[]" /></td>
<td><input type="text" class="widefat" name="height[]" /></td>
<td><a class="sort">|||</a></td>
</tr>
<?php endif; ?>
<!-- empty hidden one for jQuery -->
<tr class="empty-row screen-reader-text">
<td><a class="button remove-row" href="#">-</a></td>
<td><input type="text" class="widefat" name="text[]" /></td>
<td><input type="text" class="widefat" name="fx[]" /></td>
<td><input type="text" class="widefat" name="easing[]" /></td>
<td><input type="text" class="widefat" name="speed[]" /></td>
<td><input type="text" class="widefat" name="x[]" /></td>
<td><input type="text" class="widefat" name="y[]" /></td>
<td><input type="text" class="widefat" name="width[]" /></td>
<td><input type="text" class="widefat" name="height[]" /></td>
<td><a class="sort">|||</a></td>
</tr>
</tbody>
</table>
<p><a id="add-row" class="button" href="#">Add another</a>
<input type="submit" class="metabox_submit" value="Save" />
</p>
<?php
}
add_action('save_post', 'repeatable_meta_box_save');
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();
$text = $_POST['text'];
$fx = $_POST['fx'];
$easing = $_POST['easing'];
$speed = $_POST['speed'];
$x = $_POST['x'];
$y = $_POST['y'];
$width = $_POST['width'];
$height = $_POST['height'];
$count = count( $text );
for ( $i = 0; $i < $count; $i++ ) {
if ( $text[$i] != '' ) :
$new[$i]['text'] = stripslashes( strip_tags( $text[$i] ) );
elseif ( $fx[$i] != '' ) :
$new[$i]['fx'] = stripslashes( strip_tags( $fx[$i] ) );
elseif ( $easing[$i] != '' ) :
$new[$i]['easing'] = stripslashes( strip_tags( $easing[$i] ) );
elseif ( $speed[$i] != '' ) :
$new[$i]['speed'] = stripslashes( strip_tags( $speed[$i] ) );
elseif ( $x[$i] != '' ) :
$new[$i]['x'] = stripslashes( strip_tags( $x[$i] ) );
elseif ( $y[$i] != '' ) :
$new[$i]['y'] = stripslashes( strip_tags( $y[$i] ) );
elseif ( $width[$i] != '' ) :
$new[$i]['width'] = stripslashes( strip_tags( $width[$i] ) );
elseif ( $height[$i] != '' ) :
$new[$i]['height'] = stripslashes( strip_tags( $height[$i] ) );
endif;
}
if ( !empty( $new ) && $new != $old )
update_post_meta( $post_id, 'repeatable_fields[$i]', $new );
elseif ( empty($new) && $old )
delete_post_meta( $post_id, 'repeatable_fields[$i]', $old );
}
L'utilisation du code suivant vous permet de récupérer le tableau.
foreach ($repeatable_fields as $v) {
}
Ensuite, vous pouvez accéder à des valeurs individuelles avec quelque chose comme ceci:
<?php echo $v['text']; ?>
Peut-être que vous le sauvegardez mal, changez ceci:
if ( !empty( $new ) && $new != $old )
update_post_meta( $post_id, 'repeatable_fields[$i]', $new );
elseif ( empty($new) && $old )
delete_post_meta( $post_id, 'repeatable_fields[$i]', $old );
pour ça:
if ( !empty( $new ) && $new != $old )
update_post_meta( $post_id, 'repeatable_fields', $new );
elseif ( empty($new) && $old )
delete_post_meta( $post_id, 'repeatable_fields', $old );