WPALLIMPORT Exporting images from repeater field

I am getting this ouput when I export the custom field image,

s:267:"a:9:{s:10:"sm-field-0";s:4:"1228";s:10:"sm-field-1";s:4:"1229";s:10:"sm-field-2";s:4:"1230";s:10:"sm-field-3";s:4:"1231";s:10:"sm-field-4";s:4:"1232";s:10:"sm-field-5";s:4:"1233";s:10:"sm-field-6";s:4:"1234";s:10:"sm-field-7";s:4:"1235";s:10:"sm-field-8";s:4:"1236";}";

I can add a function or php to covert this to URLS? but I have no idea. Can anyone help?

Answers 1

  • That is a serialized string, actually it's a serialized string of a serialized string, which is very odd. You need to convert it to an array and then get the image URLs from the IDs that it is returning.

    Lets say $str is the variable that contains that string, here's how you would get the image URLs from those attachment IDs. FYI I put the second unserialize call inside an if statement so it won't break if your value ever becomes just a single serialized string

    function parseUrlsFromSerializedIds($str) {
        $arr = unserialize($str);
        if (is_serialized($arr)) {
            $arr = unserialize($arr);
        }
        $results = [];
        foreach ($arr as $imageID) {
            $url = wp_get_attachment_url($imageID);
            $results[] = $url;
        }
        return implode(',', $results);
    
    }
    

    Inside that foreach you'll want to do something with the URL. I'm not sure what format you're trying to return them all in so I left it alone.


Related Questions