i have a group of metaboxes which i finally are now saving but am struggling to get the saved values back into the input boxes they came from. so for example i have a meta field called link text and if i put 'link 1 text' in there and save I save it but it doesn't show back in the form.
the code i use for this is:
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]['image1']) ? esc_attr($values['imagefields'][$i]['image1'][0]): '';
$imagefield2[$i] = isset($values['imagefields'][$i]['image2']) ? 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] . '" />';
// var_dump($values['imagefields'][0]);
}
}
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 know the fields save because if i do a var_dump($values['linktext']);
this is the array that is returned:
array(1) { [0]=> 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 am unsure how to get the saved values back into each field.
the other small question i have is to retreieve the values of each meta box on the front end would i use:
get_post_meta($post_id, 'linktext'][1]', true);
thanks in advance for any help.