I am trying to create a custom meta box with 12 fields. There are 3 groups of 4 fields, so I am using a for loop to create them to save on typing etc.
For some reason I can't seem to get them to save. I am not sure if my coding is correct? I checked to see if they were being saved by doing a var dump of var_dump($linktext[1]);
to see if it was being saved and that returned null.
This is the call back for the add metabox to output the fields in the back end in the meta box:
function home_callout_meta_box_cb($post) {
global $post;
wp_nonce_field('home_callout_nonce', 'home_callout_meta_box_nonce');
$values = get_post_custom($post->ID);
for ($i = 1; $i <= 3; $i++) {
$imagefield1[$i] = isset($values['imagefields'][$i]) ? esc_attr($values['imagefields'][$i]['image1'][0]): '';
$imagefield2[$i] = isset($values['imagefields'][$i]) ? esc_attr($values['imagefields'][$i]['image2'][0]): '';
$linktext[$i] = isset($values['linktext'][$i]) ? esc_attr($values['linktext'][$i][0]): '';
$link[$i] = isset($values['link'][$i]) ? esc_attr($values['link'][$i][0]): '';
echo '<p><strong>Call out box ' . $i .' image 1</strong></p>';
echo '<div>';
echo '<input type="text" id="imagefields'. [$i] . '[image1]" name="imagefields' . [$i] . '[image1]" class="upload-url image_url" readonly="readonly" value="' . $imagefield1[$i] .'" />';
echo '<input id="st_upload_button" class="st_upload_button button-secondary upload_image" type="button" name="upload_button" value="Upload" />';
echo '</div>';
echo '<p><strong>Call out box ' . $i .' image 2</strong></p>';
echo '<div>';
echo '<input type="text" id="imagefields' . [$i] . '[image2]" name="imagefields' . [$i] . '][image2]" class="upload-url image_url" readonly="readonly" value="' . $imagefield2[$i] .'" />';
echo '<input id="st_upload_button" class="st_upload_button button-secondary upload_image" type="button" name="upload_button" value="Upload" />';
echo '</div>';
echo '<p><strong>Call out box ' . $i .' link text</strong></p>';
echo '<input type="text" id="linktext' . [$i] . '" name="linktext' . [$i] . '" value="' . $linktext[$i] . '" />';
echo '<p><strong>Call out box ' . $i .' link</strong></p>';
echo '<input type="text" id="link' . [$i] . '" name="link' . [$i] . '" value="' . $link[$i] . '" />';
}
}
and this is my save function:
function home_callout_save($post_id) {
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if(!isset($_POST['home_callout_meta_box_nonce']) || !wp_verify_nonce($_POST['home_callout_meta_box_nonce'], 'home_callout_nonce')) return;
if(!current_user_can('edit_pages')) return;
if(isset($_POST['imagefields'])) {
update_post_meta($post_id, 'imagefields', $_POST['imagefields']);
}
if(isset($_POST['linktext'])) {
update_post_meta($post_id, 'linktext', $_POST['linktext']);
}
if(isset($_POST['link'])) {
update_post_meta($post_id, 'link', $_POST['link']);
}
}
add_action('save_post', 'home_callout_save');
I think I've been staring at this same code for so long I think it might be a small mistake I've made and can't seem to see it.
Thanks for reading and any help is very much appreciated.
Edit:
Here is what I get from a vardump of $values['linktext']
:
string(75) "a:3:{i:1;s:11:"link text 1";i:2;s:11:"Link 2 text";i:3;s:11:"Link 3 text";}"
I cant see how to put that returned value into the fields.