Skip to content

Instantly share code, notes, and snippets.

@millipedia
Created September 10, 2021 11:47
Show Gist options
  • Save millipedia/ad43143f33ea532cee79d9442fdf54d3 to your computer and use it in GitHub Desktop.
Save millipedia/ad43143f33ea532cee79d9442fdf54d3 to your computer and use it in GitHub Desktop.
Processwire example import scrip
<?php namespace ProcessWire;
/**
*
* Import images from a CSV from WP (or wherever).
* In this case CSV has image path and page alias.
*
*/
include("index.php");
echo '<table><thead><tr><th>Exists</th><th>Page name</th><th>image</th></tr></thead>';
echo '<tbody>';
if (($handle = fopen("thumbails-201109.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$pn=$data[1];
echo '<tr>';
if($pn){
$p=$pages->get("template=post,name={$pn}");
if($p->id){
if($p->page_featured_image){
// ok - if we don't already have an image try and add the one listed in the CSV
if(!$p->page_featured_image->count){
echo '<td>OK </td>' . PHP_EOL;
echo '<td>' . $pn . '</td>' . PHP_EOL;
$p->setOutputFormatting(false);
$p->page_featured_image->removeAll();
$p->page_featured_image->add("{$data[0]}");
$p->save();
echo '<td>Added: ' . $data[0] . '</td>' . PHP_EOL;
}
}
}else{
echo '<td>404 </td>' . PHP_EOL;
echo '<td>' . $pn . '</td>' . PHP_EOL;
echo '<td></td>' . PHP_EOL;
}
}
echo '</tr>';
}
fclose($handle);
}else{
echo 'cant open thumbmnails.csv';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment