please try this code. this code will create meta boxes dynamically
<?php
add_action( 'add_meta_boxes', 'dynamic_add_custom_box' );
/* Adds a box to the main column on the Post and Page edit screens */
function dynamic_add_custom_box() {
add_meta_box(
'dynamic_sectionid',
__( 'Shortcode Information', 'myplugin_textdomain' ),
'dynamic_inner_custom_box',
'post');}
Please add slug name of Custom post in above function in the place of post
/* Prints the box content */
function dynamic_inner_custom_box() {
global $post;
wp_nonce_field( plugin_basename( __FILE__ ), 'dynamicMeta_noncename' );
$shortcode = get_post_meta($post->ID,'shortcode',true);
$c = 0;
if ( count( $shortcode ) > 0 ) {
if(is_array($shortcode)){
foreach( $shortcode as $track ) {
if ( isset( $track['toggle_title'] ) || isset( $track['toggle_title_close'] ) ) {
printf( '<div class="myshortcode_div" style="background-color: rgb(223, 223, 223); padding: 10px 20px;margin-bottom: 15px;">
<h1>Shortcode</h1><br/>
Title Open :<br/> <input type="text" name="shortcode[%1$s][toggle_title]" value="%2$s" /><br/>
Title Close :<br/> <input type="text" name="shortcode[%1$s][toggle_title_close]" value="%3$s" /><br/>
<span class="remove" id="remove_shortcode">%8$s</span></div>', $c, $track['toggle_title'], $track['toggle_title_close'], $track['toggle_hide'], $track['toggle_border'], $track['include_excerpt_html'], $track['shortcode_content'], __( 'Remove Shortcode' ));
$c = $c +1;
}
}
}
}
?>
<span id="here"></span>
<span class="add" id="add_shortcode"><?php _e('Add Shortcode'); ?></span>
<script>
var $ =jQuery.noConflict();
$(document).ready(function() {
var count = <?php echo $c; ?>;
$(".add").click(function() {
count = count + 1;
$('#here').append('<div class="myshortcode_div" style="background-color: rgb(223, 223, 223); padding: 10px 20px;margin-bottom: 15px;">\n\
<h1>Shortcode</h1><br/>\n\
<b>Title Open :</b><br/> <input type="text" name="shortcode['+count+'][toggle_title]" value="" /><br/>\n\
<b>Title Close :</b><br/> <input type="text" name="shortcode['+count+'][toggle_title_close]" value="" /><br/> \n\
<span class="remove" id="remove_shortcode">Remove Shortcode</span></div>' );
return false;
});
$(".remove").live('click', function() {
$(this).parent().remove();
});
});
</script>
/* Do something with the data entered */
add_action( 'save_post', 'dynamic_save_postdata' );
/* When the post is saved, saves our custom data */
function dynamic_save_postdata( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !isset( $_POST['dynamicMeta_noncename'] ) )
return;
if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
return;
$shortcode = $_POST['shortcode'];
update_post_meta($post_id,'shortcode',$shortcode);
}
Please try this code snippet if it works for you.