Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 ive been staring at this same code for so long i think it might be a small mistake ive 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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You are accessing the $_POST array in a wrong way. This is not possible:

$_POST['imagefields[' . $i . '][image2]']

Correct way would be:

$_POST['imagefields'][$i]['image2'];

(You have the same mistake when accessing the $values array.)

Also you are supposed to give the complete array to the update_post_meta() function instead of saving each value seperately:

 if(isset($_POST['imagefields'])) {
    update_post_meta($post_id, 'imagefields', $_POST['imagefields']);
}

*EDIT*

You can see if the values where saved to post_meta correctly using this code:

$values = get_post_custom($post->ID);
var_dump($values);

To fix the $values part you need to change all occurences of:

$values['imagefields[' . $i . '][image1]']

To:

$values['imagefields'][$i]['image1']
share|improve this answer
    
Oh okay thanks for answering. I'm not able to test out but actually makes a lot of sense. I will try it out tonight and accept as soon. Thank you –  asaunders Oct 11 '13 at 13:43
    
Hi thanks for responding again. I think it is now saving but i cant get the values to appear in the text boxes afterwards. could you just clarify how to fix my mistake on the $values part please. So i am var dumping var_dump($values['imagefields']); and i can see the full array if i add [0] i can get the first one in the array. but if i add [$i] it throws null? –  asaunders Oct 11 '13 at 17:59
    
@user720414 I updated my answer. I hope you can make it work now. –  s1lv3r Oct 11 '13 at 18:08
    
Hi thanks again for updating. I am away from my pc now but will try it tomorrow. Also does image2 need to be in quotes when I do the post –  asaunders Oct 11 '13 at 18:38
    
Generally everything that is a string needs to be in quotes and everything that is a variable (starting with $) should not be in quotes. So you are right and 'image2' should be in quotes. I edited this now. –  s1lv3r Oct 11 '13 at 18:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.