My Blog
WP All Import PHP function: Upload images and return id of images
Gift for WordPress friends who Import with WP All Import 😉
If you're using a theme or plugin that doesn't get the extra images in the classic way (except 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:
//Add gallery images function and return id for 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 over and over 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> //Add gallery images function 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 } } //add gallery images function and return id for custom field -- END
This post is also available in: Ελληνικά