Im wondering if someone can help me out … At the moment i have the following code which adds and saves data from a custom meta box in the wordpress admin.
What i want to be able to do, is add a little href that says something like “add more info”, which will then render another round of the fields, so i will be able to add as many as i need.
I hope someone can help.
The code i have to render and save just 1 round of fields is as follows:
$new_copy_prefix = 'new_copy_';
$new_copy_meta_box = array(
'id' => 'new_copy_meta_box',
'title' => 'Content',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Title:',
'desc' => '',
'id' => $new_copy_prefix . 'title',
'type' => 'text',
'std' => ''
),
array(
'name' => 'Content:',
'desc' => '',
'id' => $new_copy_prefix . 'content',
'type' => 'textarea',
'std' => ''
),
)
);
function new_copy_meta_box() {
global $new_copy_meta_box;
add_meta_box($new_copy_meta_box['id'], $new_copy_meta_box['title'], 'display_new_copy_html', $new_copy_meta_box['page'], $new_copy_meta_box['context'], $new_copy_meta_box['priority'] );
}
add_action( 'admin_menu', 'new_copy_meta_box' );
function display_new_copy_html() {
global $new_copy_meta_box, $post;
// Use nonce for verification to check that the person has adequate priveleges
echo '<input type="hidden" name="new_copy_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
// Create the code to display teh HTML
echo '<table class="form-table">';
foreach($new_copy_meta_box['fields'] as $field) {
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>';
echo ' <th style="width:35%"><label for="', $field['id'], '">', $field['name'], '</label></th>';
echo ' <td>';
switch ($field['type']) {
case 'text': // The html to display for the text type
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
break;
case 'textarea': // The html to display for the textarea type
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
break;
}
echo ' </td>';
echo '</tr>';
}
echo '</table>';
}
function new_copy_meta_box_save_data($post_id) {
global $new_copy_meta_box;
// Verify nonce -- Checks that the user has access
if (!wp_verify_nonce($_POST['new_copy_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// Check autosave
if (defined('DOING AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('post' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($new_copy_meta_box['fields'] as $field) { // Save each option
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if($new && $new != $old) { // Compare changes to existing data
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
add_action('save_post', 'new_copy_meta_box_save_data' ); // Save the data
you mean something like this ?
EDIT I (After comment )
copy and pasted from the my own answer at the above link :
you can do it in a few ways, one is adding another instance to the array (push) and ajax reload.
But a better way IMHO would be to use some dedicated metabox class – like the wp-alchemy-metaboxes class. then use one of it´s functions that you can find HERE.
Hope it will help you.