Gift for WordPress friends who Import with WP All Import 😉
In case you’re using a theme or plugin that doesn’t take the extra images in the classic way (apart from the featured image), then you’ll probably need to somehow store the images’ ids or maybe even the entire uploaded url .
Using the below function I made, you can upload the images on import and return the image ids separated by “|” in the special field.
The function is very easy and you can change it as you wish, even if it returns something else.
If you need help, please contact me.
Function:
//Function upload images on gallery and return id for your custom field function imageids($url,$url2,$url3,$url4,$url5,$url6,$url7,$url8,$url9,$url10,$url11, $url12,$url13,$url14,$url15,$url16,$url17,$url18,$url19,$url20,$url21,$url22,$url23,$url24,$url25,$url26,$url27,$url28,$url29,$url30){ require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/image.php'); $urls = array_filter(array($url,$url2,$url3,$url4,$url5,$url6,$url7,$url8,$url9,$url10,$url11, $url12,$url13,$url14,$url15,$url16,$url17,$url18,$url19,$url20,$url21,$url22,$url23,$url24,$url25,$url26,$url27,$url28,$url29,$url30)); if(empty($urls)){ //nothing }elseif(sizeof($urls) > 1) { foreach ($urls as $url){ $src = media_sideload_image( $url, null, null, 'src' ); $image_id = attachment_url_to_postid( $src ); $apotelesma .= $image_id."|"; } return substr($apotelesma,0,-1); }else{ foreach ($urls as $url){ $src = media_sideload_image( $url, null, null, 'src' ); $image_id = attachment_url_to_postid( $src ); $apotelesma = $image_id; } return $apotelesma; } }
Update!!!
You may encounter the following problem: When updating already uploaded posts, even if you choose not to update the custom field with the images, the code will run normally and download the images again and again. That’s why I added an extra check.
The following function checks the database to see if the ID of the post already exists in the database, and if so, then it does not proceed with the execution of the rest of the function:
</pre> //Function upload images on gallery and return id for custom field -- START function imageids($elegxos,$url,$url2,$url3,$url4,$url5,$url6,$url7,$url8,$url9,$url10,$url11,$url12, $url13,$url14,$url15,$url16,$url17,$url18,$url19,$url20,$url21,$url22,$url23,$url24,$url25,$url26,$url27,$url28,$url29,$url30){ global $wpdb; $metas = $wpdb->get_results( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'real_estate_property_identity' AND meta_value = %s", $elegxos) ); if(empty($metas)){ require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/image.php'); $urls = array_filter(array($url,$url2,$url3,$url4,$url5,$url6,$url7,$url8,$url9,$url10,$url11,$url12, $url13,$url14,$url15,$url16,$url17,$url18,$url19,$url20,$url21,$url22,$url23,$url24,$url25,$url26,$url27,$url28,$url29,$url30)); if(empty($urls)){ //nothing }elseif(sizeof($urls) > 1) { foreach ($urls as $url){ $src = media_sideload_image( $url, null, null, 'src' ); $image_id = attachment_url_to_postid( $src ); $apotelesma .= $image_id."|"; } return substr($apotelesma,0,-1); }else{ foreach ($urls as $url){ $src = media_sideload_image( $url, null, null, 'src' ); $image_id = attachment_url_to_postid( $src ); $apotelesma = $image_id; } return $apotelesma; } }else{ //nothing } } //Function upload images on gallery and return id for custom field -- END
Original Source: https://nicolaslagios.com/wp-all-import-%cf%83%cf%85%ce%bd%ce%ac%cf%81%cf%84%ce%b7%cf%83%ce%b7-php-%ce%b1%ce%bd%ce%ad%ce%b2%ce%b1%cf%83%ce%bc%ce%b1-%ce%b5%ce%b9%ce%ba%cf%8c%ce%bd%cf%89%ce%bd-%ce%ba%ce%b1%ce%b9-%ce%b5%cf%80/