Loading 0
Share

My Blog

Scroll Down

WP All Import PHP function: Upload images and return id of images

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: Ελληνικά

Leave a Reply

Your email address will not be published. Required fields are marked *

01.

Here you can see all the services I provide

Registration and management of domain names (website address such as www.nicolaslagios.com)

Also management of dns records (e.g. connecting the domain to a specific server, fixing email spam problems, etc.)

Also ssl renewals etc

Installation and management of web & mail server in ubuntu vps with virtualmin, plesk, cpanel

Also studying and fixing server problems.

Necessary condition, the target server meets the conditions

At the moment for new wordpress websites you can choose from ready-made themes and we change the content (no custom changes). You can buy with a fixed price by clicking here!

My team and I undertake any data bridging implementation for Wordpress, Prestashop, Opencart, Joomla platforms.

We can connect data from any source, as long as the structure is stable and there is proper documentation and briefing.

We undertake the creation, regulation and enrichment of pages for social networks: Facebook, Linkedin, Instagram (profile), Twitter (profile), Tiktok (profile).

We also undertake the first boost of your pages for quick results in followers.

We undertake the repair and maintenance of your existing wordpress website.

For more information about the services, you can read the following and return here to schedule a meeting with me: https://maxservices.gr/en/internet-services/website-services-blank/additional-website-services/