{"id":9501,"date":"2022-01-23T22:50:47","date_gmt":"2022-01-23T20:50:47","guid":{"rendered":"https:\/\/maxservices.gr\/wp-all-import-php-function-upload-images-and-return-id-of-images\/"},"modified":"2022-01-23T22:50:47","modified_gmt":"2022-01-23T20:50:47","slug":"wp-all-import-php-function-upload-images-and-return-id-of-images","status":"publish","type":"post","link":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/","title":{"rendered":"WP All Import PHP function: Upload images and return id of images"},"content":{"rendered":"
Gift for WordPress friends who Import with WP All Import \ud83d\ude09<\/p>\n
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 .<\/p>\n
Using the below function I made, you can upload the images on import and return the image ids separated by “|” in the special field.<\/p>\n
The function is very easy and you can change it as you wish, even if it returns something else.<\/p>\n
If you need help, please contact me.<\/p>\n
Function:<\/p>\n
\n\n\/\/Function upload images on gallery and return id for your custom field\nfunction 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){\n\nrequire_once(ABSPATH . 'wp-admin\/includes\/media.php');\nrequire_once(ABSPATH . 'wp-admin\/includes\/file.php');\nrequire_once(ABSPATH . 'wp-admin\/includes\/image.php');\n\n$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));\n\nif(empty($urls)){\n\/\/nothing\n}elseif(sizeof($urls) > 1) {\nforeach ($urls as $url){\n$src = media_sideload_image( $url, null, null, 'src' );\n$image_id = attachment_url_to_postid( $src );\n$apotelesma .= $image_id."|";\n}\nreturn substr($apotelesma,0,-1);\n}else{\nforeach ($urls as $url){\n$src = media_sideload_image( $url, null, null, 'src' );\n$image_id = attachment_url_to_postid( $src );\n$apotelesma = $image_id;\n}\nreturn $apotelesma;\n}\n}\n\n<\/pre>\nUpdate!!!
\nYou 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.
\nThe 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:<\/p>\n<\/pre>\n\/\/Function upload images on gallery and return id for custom field -- START\nfunction imageids($elegxos,$url,$url2,$url3,$url4,$url5,$url6,$url7,$url8,$url9,$url10,$url11,$url12,\n$url13,$url14,$url15,$url16,$url17,$url18,$url19,$url20,$url21,$url22,$url23,$url24,$url25,$url26,$url27,$url28,$url29,$url30){\nglobal $wpdb;\n\n$metas = $wpdb->get_results(\n$wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'real_estate_property_identity' AND meta_value = %s", $elegxos)\n);\n\nif(empty($metas)){\nrequire_once(ABSPATH . 'wp-admin\/includes\/media.php');\nrequire_once(ABSPATH . 'wp-admin\/includes\/file.php');\nrequire_once(ABSPATH . 'wp-admin\/includes\/image.php');\n$urls = array_filter(array($url,$url2,$url3,$url4,$url5,$url6,$url7,$url8,$url9,$url10,$url11,$url12,\n$url13,$url14,$url15,$url16,$url17,$url18,$url19,$url20,$url21,$url22,$url23,$url24,$url25,$url26,$url27,$url28,$url29,$url30));\nif(empty($urls)){\n\/\/nothing\n}elseif(sizeof($urls) > 1) {\nforeach ($urls as $url){\n$src = media_sideload_image( $url, null, null, 'src' );\n$image_id = attachment_url_to_postid( $src );\n$apotelesma .= $image_id."|";\n}\nreturn substr($apotelesma,0,-1);\n}else{\nforeach ($urls as $url){\n$src = media_sideload_image( $url, null, null, 'src' );\n$image_id = attachment_url_to_postid( $src );\n$apotelesma = $image_id;\n}\nreturn $apotelesma;\n}\n}else{\n\/\/nothing\n}\n\n}\n\/\/Function upload images on gallery and return id for custom field -- END<\/pre>\nOriginal 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\/<\/p>\n","protected":false},"excerpt":{"rendered":"
Gift for WordPress friends who Import with WP All Import \ud83d\ude09 In case you’re using a theme or plugin that doesn’t take the extra images <\/p>\n","protected":false},"author":1,"featured_media":8372,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[236,582,590,580,584],"tags":[591,585],"acf":[],"yoast_head":"\n
WP All Import PHP function: Upload images and return id of images - Max Services<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n