How to import images from URLs into your WordPress Pods
Importing external images into your WordPress media library is a fairly common task when porting a site from one system to another.
Or maybe you’re filling your database from a .CSV spreadsheet, and want to fill it as well with profile images or product images.
So provided you’ve set up your Pod already, and it has an image field present, here’s how to import an image programmatically to your pod from an external url :
$item = array();
$item["image_field"] = pods_attachment_import($url_to_your_image);
pods('your_pod')->add($item);
This way your image will be properly added to the WordPress media library, and won’t be loaded from the old url.
A little bit more extensive example, let’s assume you’re importing something from a cars.csv
file with the fields name,image_url
and want to add it to a cars pod that has the name
and image
fields :
<?php
$csv = array_map('str_getcsv', file("cars.csv"));
array_walk($csv, function(&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});
array_shift($csv); # remove column header
foreach ($csv as $index => $row) {
$item = array();
$item["name"] = $row["name"];
if($row["image_url"]) {
$item["image"] = pods_attachment_import($row["image_url"]);
}
pods('cars')->add($item);
}
After executing this, your cars
pod should be filled now with the values from your cars.csv
file.