0

I am working on my first plugin. I've got a custom post type, with a meta box which has 4 fields which uses update_post_meta, & these work 100% fine. I have then tried to create a meta box that has repeatable fields, when the user clicks 'add new field'. I want users to be able to add as many fields as they like.

My Question: Below, is my code to add new fields. This just doesn't display in the custom post type when viewed, but I don't get any errors. Please can you have a look at my code below, & advise?

<?php
// START OF CREATE MORE META BOXES AS NEEDED INCLUDING JQUERY 
add_action( 'add_meta_boxes', 'dynamic_add_custom_box' );

/* Do something with the data entered */
add_action( 'save_post', 'dynamic_save_postmeta' );

/* Adds a box to the main column on the post and page edit screens */

function dynamic_add_custom_box() {
    add_meta_box(
            'dynamic_sectionid', 'My Tracks', 'mp_myplugin_textdomain', 'mp_dynamic_inner_custom_box', 'post' );
}

/* Prints the box content  */

function mp_dynamic_inner_custom_box() {
    global $post;
    ?> 
    <div id="meta_inner"> 
        <?php
        // get the saved meta as an array 
        $mpformfield = get_post_meta( $post->ID, 'mpformfield', true );
        $c = 0;
        if ( count( $mpformfield ) > 0 ) {
            foreach ( $mpformfield as $mpfield ) {
                if ( isset( $mpfield['name'] ) ) {
                    printf( '<p> Field Name <input type="text" name="fieldname[%1$s][name]" value="%2$s" /> <br /> </p>' );
                }
            }
        }
        ?>
        <span id="here"></span> 
        <span class="add"><?php 'Add form field'; ?> </span> 
        <script>  
            var $ = jQuery.noConflict(); 
            $(document).ready(function() { 
                var count =  <?php echo $c; ?>; 
                $(".add").click(function() { 
                    count = count + 1; 

                    $('#here').append('<p> Form field <input type="text" name="fieldname['+count+'][name]"  value="" /><span class="remove"> Remove form field </span>  </p>'); 
                    return false; 
                }); 
                $(".remove").live('click', function() { 
                    $(this).parent().remove(); 
                }); 
            }); 
        </script>
    </div> <?php
}

/* when the post is saved, saves our custom data */

function dynamic_save_postmeta( $post_id ) {
    // verify if this is an auto save routine 
    // if it is our form has not been submitted, so we don't want to do anything 
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    // Save the data 
    $mpformfield = $_POST['mpformfield'];
    update_post_meta( $post_id, 'mpformfield', $mpformfield );
}
7
  • Your code is full of break tags. Please clean it up.
    – vancoder
    Commented Apr 11, 2013 at 17:57
  • @vancoder Realised straight away, & just finished cleaning it up. Thanks for taking an interest!
    – user28566
    Commented Apr 11, 2013 at 17:59
  • Are you sure you aren't getting javascript errors?
    – vancoder
    Commented Apr 11, 2013 at 18:04
  • @vancoder There are javascript errors (that's how much of a rookie I am) . Thank-you!!
    – user28566
    Commented Apr 11, 2013 at 18:25
  • You are adding the meta box only to the post post type. You need one add_meta_box per post type. What do you mean by "blank page", it is simply that the meta box doesn't show up or the code breaks the site and you have a blank screen when trying to access the page? The concept of "repeatable meta boxes" is not correct, what you can have are repeatable fields inside a meta box. Check this search query for many working examples.
    – brasofilo
    Commented Apr 11, 2013 at 18:47

1 Answer 1

0

To add a meta box to multiple post types, one add_meta_box per post type is needed. In this example, instead of duplicating the same code, a foreach is used. More post types can be added to the array and the meta box will be added in all of them:

add_action( 'add_meta_boxes', 'add_custom_box_wpse_94701' );

function add_custom_box_wpse_94701() 
{
    // Post types to insert the meta box. Adjust array <-------
    foreach( array( 'post', 'portfolio' ) as $pt )
        add_meta_box(
            'sectionid_wpse_94701',
            __( 'Custom parent' ), 
            'blogroll_box_wpse_94701',
            $pt,
            'side'
        );
}

The example above is taken from this Q&A: List of Posts in a Custom Field

Note that the action hook save_post takes 2 parameters, $post_id and $post_object. The latter contains all the post data (title, content, post type, etc).

There are other checks that can be done inside save_post. Check this search query for examples of adding meta boxes and saving the post data.

Your Answer

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