I'm building a custom post type in Wordpress which will be used to author and display BuzzFeed style quizzes on a clients blog. Within the custom post type there are custom meta-boxes which represent each question and selections, and another representing the potential ultimate results.

So the first meta-box consists of a number input type to assign an ID, a textfield to enter the question, and another field to enter an image. To the author this would represent the first question in their quiz. If they have more than one question to enter, they would need to select the button to add another set of fields.

What I'm not sure how to do is how to generate that and ensure that any added fields get saved. I'd rather not resort to a plugin or a drop in library as it's important that I learn how to do it as much as it is that I resolve it.


1 Answer 1


please try this code. this code will create meta boxes dynamically

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() {
    __( 'Shortcode Information', 'myplugin_textdomain' ),

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 ) {
    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;">
                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>

    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\
                           <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() {

/* 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 ) 
    if ( !isset( $_POST['dynamicMeta_noncename'] ) )

if ( !wp_verify_nonce( $_POST['dynamicMeta_noncename'], plugin_basename( __FILE__ ) ) )
    $shortcode = $_POST['shortcode'];


Please try this code snippet if it works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.