• Resolved Gisele

    (@gislef)


    There are two images that I manually put in the database in the table wp_postmeta, with the value of the meta key: my-image-for-post, indicating Of course the id of the post:

    I can print in the metabox those images that are already in the DB when editing the post. Like this:

    $urlsDB =get_post_meta($post->ID,'my-image-for-post', false);
    
     foreach ($urlsDB as $url1) { 
     ?>
    
             <img src="<?php echo $url1;?>" style="width:200px;" />
    
     <?php
     };

    But I am trying to save the many images that I select in the metabox when editing the post.

    I can select images with wordpress image manager and with jquery fill in the urls and insert name='my_image_URL' into the input of each selected image:

    $("#show").after("<input class='upload_image' type='text' size='36' name='my_image_URL' value= " +attachment.url+ " /></br><img src=" +attachment.url+" class='my_image' src='' width='80px'></br>");

    Inside the <div id="show"> displays the selected images and the inputs are filled with the url of each image to be saved.

    The meta box is working OK, but when I save the post the content changes, but it does not save the metabox images in DB, I tried the two ways below:

    1

    add_action( 'save_post', function ($post_id) {
    
        if (isset($_POST['my_image_URL'])){
                $urls = $_POST['my_image_URL'];
    
                Foreach ($urls as $url){
            $key1_values = get_post_custom_values( 'my-image-for-post', $post_id );
            Foreach($key1_values as $value){
                update_post_meta($post_id, 'my-image-for-post', $url, $value);
            }
            }
       }
    
    });

    2nd way I’ve tried

    add_action( 'save_post', function ($post_id) {
    
            if (isset($_POST['my_image_URL'])){
            $urls = $_POST['my_image_URL'];
    
             Foreach ($urls as $url){
                update_post_meta($post_id, 'my-image-for-post',$url);
            }
        }
    
    });

    But do not save the urls in the wp_postmeta table, what can I be doing wrong?

    Thanks

    • This topic was modified 8 years, 1 month ago by Gisele.
    • This topic was modified 8 years, 1 month ago by Gisele.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You cannot have multiple input elements with the same name. You end up with only the last value in $_POST[‘my_image_URL’]. Instead, use the name “my_image_URL[]”. This will collect all the field values into an array. OK, that’s still the same name in each field, but the square brackets cause an array to be built, so it’s as though there was a unique index value inside of each bracket, so not really the same name internally.

    Also, don’t loop through the array to save individual values, these are difficult to manage. Just save the entire array as a single meta value. The array is automatically serialized and unserialized, so when you get the meta value, you already have the full array ready to loop through.

    Thread Starter Gisele

    (@gislef)

    Thanks its worked for me, but how do I edit (update, delete or add) just one or a few value fields?

    How do I specifically access some of the data that is serialized in DB?

    Moderator bcworkz

    (@bcworkz)

    You can edit the input fields in the meta box. The image will not change without some javascript manipulation, but when you save the post the current field values are saved and when the page reloads the correct image will display.

    You can manipulate the data by code by accessing individual array elements before saving in post meta. It’s a standard indexed array. Same when it’s retrieved. Data within any element can be altered prior to output.

    You can also directly alter the serialized data through phpMyAdmin, but it’s not recommended because it’s very easy to corrupt the structure. Minor edits are possible if you take care to update the associated string size to match the edit. More elaborate edits would be a bad idea. Either way, backup the table before doing anything, just in case.

    Thread Starter Gisele

    (@gislef)

    Thanks, your support helped me a lot.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to save metabox data with foreach’ is closed to new replies.