/********Add Crux/emotions field*******/
add_action('admin_init', 'c2_repet_add_meta_boxes', 2);
function c2_repet_add_meta_boxes() {
add_meta_box( 'c2-repet-group', 'Crux / Emoticons', 'c2_repet_meta_box_display', 'post', 'side', 'default');
}
function c2_repet_meta_box_display() {
global $post;
$c2Repeat_group = get_post_meta($post->ID, 'customdata_group_emo', true);
wp_nonce_field( 'c2_repeatable_meta_box_nonce', 'c2_repeatable_meta_box_nonce' );
?>
<script type="text/javascript">
jQuery(document).ready(function( $ ){
$( '#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;
});
});
</script>
<table id="repeatable-fieldset-one" width="100%">
<tbody>
<?php
if ( $c2Repeat_group ) :
foreach ( $c2Repeat_group as $field ) {
?>
<tr>
<td width="65%">
<input type="text" placeholder="Title" name="TitleItemC2[]" value="<?php if($field['TitleItemC2'] != '') echo esc_attr( $field['TitleItemC2'] ); ?>" /></td>
<td width="35%"><a class="button remove-row" href="#1">Remove</a></td>
</tr>
<?php
}
else :
// show a blank one
?>
<tr>
<td>
<input type="text" placeholder="Title" title="Title" name="TitleItemC2[]" /></td>
<td><a class="button cmb-remove-row-button button-disabled" href="#">Remove</a></td>
</tr>
<?php endif; ?>
<!-- empty hidden one for jQuery -->
<tr class="empty-row screen-reader-text">
<td>
<input type="text" placeholder="Title" name="TitleItemC2[]"/></td>
<td><a class="button remove-row" href="#">Remove</a></td>
</tr>
</tbody>
</table>
<p><a id="add-row" class="button" href="#">Add another</a></p>
<?php
}
add_action('save_post', 'custom_repeatable_meta_box_save');
function custom_repeatable_meta_box_save($post_id) {
if ( ! isset( $_POST['c2_repeatable_meta_box_nonce'] ) ||
! wp_verify_nonce( $_POST['c2_repeatable_meta_box_nonce'], 'c2_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, 'customdata_group_emo', true);
$new = array();
$emoItems = $_POST['TitleItemC2'];
$count = count( $emoItems );
for ( $i = 0; $i < $count; $i++ ) {
if ( $emoItems[$i] != '' ) :
$new[$i]['TitleItemC2'] = stripslashes( strip_tags( $emoItems[$i] ) );
endif;
}
if ( !empty( $new ) && $new != $old )
update_post_meta( $post_id, 'customdata_group_emo', $new );
elseif ( empty($new) && $old )
delete_post_meta( $post_id, 'customdata_group_emo', $old );
}
/********Add Crux/emotions field*******/
Use unserialize to get unserialize data
$post_node= (!empty($post_meta))?unserialize($post_meta):'';
Jagdish Sarma Asked question January 10, 2023