Skip to content

Instantly share code, notes, and snippets.

@AtomicSmash
Last active December 30, 2015 20:29
Show Gist options
  • Save AtomicSmash/7881010 to your computer and use it in GitHub Desktop.
Save AtomicSmash/7881010 to your computer and use it in GitHub Desktop.
Start of Wordpress import script
<?php
/*
___ __ _ __
/ | / /_____ ____ ___ (_______________ ___ ____ ______/ /_
/ /| |/ __/ __ \/ __ `__ \/ / ___/ ___/ __ `__ \/ __ `/ ___/ __ \
/ ___ / /_/ /_/ / / / / / / / /__(__ / / / / / / /_/ (__ / / / /
/_/ |_\__/\____/_/ /_/ /_/_/\___/____/_/ /_/ /_/\__,_/____/_/ /_/
Wordpress XML importer - with Advanced Custom Fields Support
*/
//Grab XML file
require_once('wp-load.php'); //Include wp-load.php, change to where
global $wp_the_query, $wp_query, $wp_rewrite, $wp;
//require_once(self::$wp_path . '/wp-load.php');
$xml=simplexml_load_file("all-local-authorities.xml");
echo "<pre>";
print_r($xml);
echo "</pre>";
//Loops through XML file
foreach($xml->Projects->Project as $project){
/*
echo "<pre>";
print_r($project);
echo "</pre>";
*/
//echo("About to import : ".$project->title."<br>");
//addPortfolioItem($project);
//updatePortfolioItem($project);
//echo("<strong>Import complete</strong><br>");
}
//Helpers
function addFeaturedImage($post_id,$imageRef){
if($imageRef != ""){
//echo($imageRef."<br>");
//$filename = $imageRef;
/*
echo "<pre>";
print_r(wp_upload_dir());
echo "</pre>";
*/
$wp_upload_dir = wp_upload_dir();
//$imageRef = $wp_upload_dir.$imageRef;
$wp_filetype = wp_check_filetype( $wp_upload_dir['baseurl'] . '/architen_files/' . basename( $imageRef ) . '.jpg' , null );
/*
echo "<pre>";
print_r($wp_filetype);
echo "</pre>";
*/
//print_r($wp_filetype);
//echo($wp_upload_dir['url']);
//echo($wp_upload_dir['baseurl'] . '/architen_files/' . basename( $imageRef ) . '.jpg<br>');
$attachment = array(
'guid' => $wp_upload_dir['baseurl'] . '/architen_files/' . basename( $imageRef ) . '.jpg',
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $imageRef ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $wp_upload_dir['basedir'] . '/architen_files/' . basename( $imageRef ) . '.jpg' );
//$images = array();
//array_push($images, $attach_id);
//print_r($images);
//echo($attach_id);
set_post_thumbnail( $post_id, $attach_id );
echo("Featured image added<br>");
}else{
echo("No featured Image set<br>");
}
};
//ACF Gallery
function addGalleryImage($post_id,$imageRef){
//$filename = $imageRef;
$wp_upload_dir = wp_upload_dir();
//$imageRef = $wp_upload_dir.$imageRef;
$wp_filetype = wp_check_filetype( $wp_upload_dir['baseurl'] . '/architen_files/' . basename( $imageRef ) . '.jpg' , null );
$attachment = array(
'guid' => $wp_upload_dir['baseurl'] . '/architen_files/' . basename( $imageRef ) . '.jpg',
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $imageRef ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $wp_upload_dir['basedir'] . '/architen_files/' . basename( $imageRef ) . '.jpg' );
//$images = array();
return $attach_id;
};
function addTax($post_id,$terms,$tax){
//echo($details->marketSector."<br>");
$cleanSectors = str_replace(array( '(' , ')' , "'" , "<br>" , "/n" ), '', $terms);
//echo("clean: ".$cleanSectors);
//var_dump($cleanSectors);
//echo(rtrim($cleanSectors, ",")."<br>");
//rtrim($string, ",")
$sectorArray = explode( ',' , $cleanSectors );
//echo "<pre>";
//var_dump($sectorArray);
//print_r(array_filter(array_map('trim', $sectorArray)));
//echo "</pre>";
wp_set_object_terms( $post_id, $sectorArray , $tax, false );
}
function addPost($article){
/*
echo "<pre>";
print_r($article);
echo "</pre>";
*/
$newText = $article->text;
$newText = strip_tags($newText,'<p><a><img>');
$post_information = array(
'post_title' => wp_strip_all_tags( $article->title ),
'post_content' => $newText,
'post_type' => 'post',
'post_status' => 'publish',
'post_date' => $newDate
);
$post_id = wp_insert_post($post_information);
addFeaturedImage($post_id,$article->image);
$post_information = array(
'post_title' => wp_strip_all_tags( $details->title ),
'post_content' => wp_strip_all_tags( $details->fullDescription ),
'post_type' => 'projects',
'post_status' => 'draft'
);
$post_id = wp_insert_post($post_information);
//echo($post_id);
//Add Category
addTax($post_id,$details->category,'project-categories');
//Add Sector
addTax($post_id,$details->marketSector,'project-sectors');
//Add summary
update_post_meta($post_id, 'project_summary', wp_strip_all_tags( $details->summary ));
//add featured image
addFeaturedImage($post_id,$details->primaryPhoto);
//add Gallery images if they exist
$images = array();
if($details->portraitPhoto1 != ""){
array_push($images, addGalleryImage($post_id,$details->portraitPhoto1));
};
if($details->portraitPhoto2 != ""){
array_push($images, addGalleryImage($post_id,$details->portraitPhoto2));
};
if($details->landscapePhoto1 != ""){
array_push($images, addGalleryImage($post_id,$details->landscapePhoto1));
};
if($details->landscapePhoto2 != ""){
array_push($images, addGalleryImage($post_id,$details->landscapePhoto2));
};
if($details->landscapePhoto3 != ""){
array_push($images, addGalleryImage($post_id,$details->landscapePhoto3));
echo "Adding gallery image";
};
if( !empty($images) ){
//echo($attach_id);
print_r($images);
update_field('field_529773c1881dd', $images, $post_id);
};
echo("<br>");
};
function updatePost($details){
//print_r($details);
$page = get_page_by_title( $details->title , OBJECT, 'projects');
echo($details->title."<br>");
//echo "<pre>";
//print_r($page);
//echo "</pre>";
if($page->ID){
//echo $page->ID."<br>";
//echo($details->fullDescription);
//echo strip_tags($details->fullDescription,'<p><b><strong><div><br><iframe>');
$my_post = array(
'ID' => $page->ID,
'post_content' => strip_tags($details->fullDescription,'<p><b><strong><div><br><iframe><a>')
);
// Update the post into the database
wp_update_post( $my_post );
};
};
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment