{"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) &gt; 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>\n

Update!!!
\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

&lt;\/pre&gt;\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-&gt;get_results(\n$wpdb-&gt;prepare("SELECT post_id FROM $wpdb-&gt;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) &gt; 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>\n

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\/<\/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":"\nWP All Import PHP function: Upload images and return id of images - Max Services<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WP All Import PHP function: Upload images and return id of images\" \/>\n<meta property=\"og:description\" content=\"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\" \/>\n<meta property=\"og:url\" content=\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/\" \/>\n<meta property=\"og:site_name\" content=\"Max Services\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-23T20:50:47+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nicolas\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Organization\",\"Place\"],\"@id\":\"https:\/\/maxservices.gr\/#organization\",\"name\":\"Max Services\",\"url\":\"https:\/\/maxservices.gr\/\",\"sameAs\":[],\"logo\":{\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#local-main-organization-logo\"},\"image\":{\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#local-main-organization-logo\"},\"openingHoursSpecification\":[{\"@type\":\"OpeningHoursSpecification\",\"dayOfWeek\":[\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\",\"Sunday\"],\"opens\":\"09:00\",\"closes\":\"17:00\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/maxservices.gr\/#website\",\"url\":\"https:\/\/maxservices.gr\/\",\"name\":\"Max Services\",\"description\":\"\u03a5\u03c0\u03b7\u03c1\u03b5\u03c3\u03af\u03b5\u03c2 \u0394\u03b9\u03b1\u03b4\u03b9\u03ba\u03c4\u03cd\u03bf\u03c5 & \u03a6\u03b9\u03bb\u03bf\u03be\u03b5\u03bd\u03af\u03b1\u03c2\",\"publisher\":{\"@id\":\"https:\/\/maxservices.gr\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/maxservices.gr\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#primaryimage\",\"url\":\"https:\/\/maxservices.gr\/wp-content\/uploads\/2022\/04\/wp-all-import.jpg\",\"contentUrl\":\"https:\/\/maxservices.gr\/wp-content\/uploads\/2022\/04\/wp-all-import.jpg\",\"width\":256,\"height\":256},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#webpage\",\"url\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/\",\"name\":\"WP All Import PHP function: Upload images and return id of images - Max Services\",\"isPartOf\":{\"@id\":\"https:\/\/maxservices.gr\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#primaryimage\"},\"datePublished\":\"2022-01-23T20:50:47+00:00\",\"dateModified\":\"2022-01-23T20:50:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/maxservices.gr\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WP All Import PHP function: Upload images and return id of images\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#webpage\"},\"author\":{\"@id\":\"https:\/\/maxservices.gr\/#\/schema\/person\/c1c4c81d69db4604ff60b48002adc5a7\"},\"headline\":\"WP All Import PHP function: Upload images and return id of images\",\"datePublished\":\"2022-01-23T20:50:47+00:00\",\"dateModified\":\"2022-01-23T20:50:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#webpage\"},\"wordCount\":637,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/maxservices.gr\/#organization\"},\"image\":{\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/maxservices.gr\/wp-content\/uploads\/2022\/04\/wp-all-import.jpg\",\"keywords\":[\"Websites & Hosting>Wordpress>WP All Import\",\"Websites & Hosting>Wordpress>Programming\"],\"articleSection\":{\"1\":\"Wordpress\",\"2\":\"WP All Import\",\"3\":\"Websites & Hosting\",\"4\":\"Programming\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#respond\"]}],\"copyrightYear\":\"2022\",\"copyrightHolder\":{\"@id\":\"https:\/\/maxservices.gr\/#organization\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/maxservices.gr\/#\/schema\/person\/c1c4c81d69db4604ff60b48002adc5a7\",\"name\":\"Nicolas\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/maxservices.gr\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8b2c794829f3cd47775142b85a598a02?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8b2c794829f3cd47775142b85a598a02?s=96&d=mm&r=g\",\"caption\":\"Nicolas\"},\"sameAs\":[\"http:\/\/maxservices.gr\"],\"url\":\"https:\/\/maxservices.gr\/en\/author\/webmaster\/\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#local-main-organization-logo\",\"url\":\"https:\/\/maxservices.gr\/wp-content\/uploads\/2022\/06\/maxservices_ig_profile.jpg\",\"contentUrl\":\"https:\/\/maxservices.gr\/wp-content\/uploads\/2022\/06\/maxservices_ig_profile.jpg\",\"width\":360,\"height\":360,\"caption\":\"Max Services\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"WP All Import PHP function: Upload images and return id of images - Max Services","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/","og_locale":"en_US","og_type":"article","og_title":"WP All Import PHP function: Upload images and return id of images","og_description":"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","og_url":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/","og_site_name":"Max Services","article_published_time":"2022-01-23T20:50:47+00:00","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Nicolas","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Organization","Place"],"@id":"https:\/\/maxservices.gr\/#organization","name":"Max Services","url":"https:\/\/maxservices.gr\/","sameAs":[],"logo":{"@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#local-main-organization-logo"},"image":{"@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#local-main-organization-logo"},"openingHoursSpecification":[{"@type":"OpeningHoursSpecification","dayOfWeek":["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],"opens":"09:00","closes":"17:00"}]},{"@type":"WebSite","@id":"https:\/\/maxservices.gr\/#website","url":"https:\/\/maxservices.gr\/","name":"Max Services","description":"\u03a5\u03c0\u03b7\u03c1\u03b5\u03c3\u03af\u03b5\u03c2 \u0394\u03b9\u03b1\u03b4\u03b9\u03ba\u03c4\u03cd\u03bf\u03c5 & \u03a6\u03b9\u03bb\u03bf\u03be\u03b5\u03bd\u03af\u03b1\u03c2","publisher":{"@id":"https:\/\/maxservices.gr\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/maxservices.gr\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#primaryimage","url":"https:\/\/maxservices.gr\/wp-content\/uploads\/2022\/04\/wp-all-import.jpg","contentUrl":"https:\/\/maxservices.gr\/wp-content\/uploads\/2022\/04\/wp-all-import.jpg","width":256,"height":256},{"@type":"WebPage","@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#webpage","url":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/","name":"WP All Import PHP function: Upload images and return id of images - Max Services","isPartOf":{"@id":"https:\/\/maxservices.gr\/#website"},"primaryImageOfPage":{"@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#primaryimage"},"datePublished":"2022-01-23T20:50:47+00:00","dateModified":"2022-01-23T20:50:47+00:00","breadcrumb":{"@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/maxservices.gr\/en\/"},{"@type":"ListItem","position":2,"name":"WP All Import PHP function: Upload images and return id of images"}]},{"@type":"Article","@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#article","isPartOf":{"@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#webpage"},"author":{"@id":"https:\/\/maxservices.gr\/#\/schema\/person\/c1c4c81d69db4604ff60b48002adc5a7"},"headline":"WP All Import PHP function: Upload images and return id of images","datePublished":"2022-01-23T20:50:47+00:00","dateModified":"2022-01-23T20:50:47+00:00","mainEntityOfPage":{"@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#webpage"},"wordCount":637,"commentCount":0,"publisher":{"@id":"https:\/\/maxservices.gr\/#organization"},"image":{"@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#primaryimage"},"thumbnailUrl":"https:\/\/maxservices.gr\/wp-content\/uploads\/2022\/04\/wp-all-import.jpg","keywords":["Websites & Hosting>Wordpress>WP All Import","Websites & Hosting>Wordpress>Programming"],"articleSection":{"1":"Wordpress","2":"WP All Import","3":"Websites & Hosting","4":"Programming"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#respond"]}],"copyrightYear":"2022","copyrightHolder":{"@id":"https:\/\/maxservices.gr\/#organization"}},{"@type":"Person","@id":"https:\/\/maxservices.gr\/#\/schema\/person\/c1c4c81d69db4604ff60b48002adc5a7","name":"Nicolas","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/maxservices.gr\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8b2c794829f3cd47775142b85a598a02?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8b2c794829f3cd47775142b85a598a02?s=96&d=mm&r=g","caption":"Nicolas"},"sameAs":["http:\/\/maxservices.gr"],"url":"https:\/\/maxservices.gr\/en\/author\/webmaster\/"},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/maxservices.gr\/en\/wp-all-import-php-function-upload-images-and-return-id-of-images\/#local-main-organization-logo","url":"https:\/\/maxservices.gr\/wp-content\/uploads\/2022\/06\/maxservices_ig_profile.jpg","contentUrl":"https:\/\/maxservices.gr\/wp-content\/uploads\/2022\/06\/maxservices_ig_profile.jpg","width":360,"height":360,"caption":"Max Services"}]}},"_links":{"self":[{"href":"https:\/\/maxservices.gr\/en\/wp-json\/wp\/v2\/posts\/9501"}],"collection":[{"href":"https:\/\/maxservices.gr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/maxservices.gr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/maxservices.gr\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/maxservices.gr\/en\/wp-json\/wp\/v2\/comments?post=9501"}],"version-history":[{"count":0,"href":"https:\/\/maxservices.gr\/en\/wp-json\/wp\/v2\/posts\/9501\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/maxservices.gr\/en\/wp-json\/wp\/v2\/media\/8372"}],"wp:attachment":[{"href":"https:\/\/maxservices.gr\/en\/wp-json\/wp\/v2\/media?parent=9501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/maxservices.gr\/en\/wp-json\/wp\/v2\/categories?post=9501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/maxservices.gr\/en\/wp-json\/wp\/v2\/tags?post=9501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}