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 );
}
post
post type. You need oneadd_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.