Skip to content

Instantly share code, notes, and snippets.

@npapratovic
Created April 29, 2020 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npapratovic/c47dedb33110aeb17cbbf0ecdca88a02 to your computer and use it in GitHub Desktop.
Save npapratovic/c47dedb33110aeb17cbbf0ecdca88a02 to your computer and use it in GitHub Desktop.
Example of custom made import script which imports content from xml file. XMl file is pulled from Access db of ASP.NET powered CMS. This exported file containes information of posts which I used to import in WordPress CMS. Script imports posts and images to gallery. Gallery is made as ACF gallery field. Take a look at this example and use it as …
<?php
/*Only for debugging..*/
error_reporting(E_ALL);
ini_set('display_errors', 'On');
ini_set('max_execution_time', 0); // for infinite time of execution
// require wp-load.php to use built-in WordPress functions
require_once '../wp-load.php';
require_once ABSPATH . 'wp-config.php';
require_once ABSPATH . 'wp-includes/wp-db.php';
require_once ABSPATH . 'wp-admin/includes/taxonomy.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
//godie.php is my custom function for debugging
require_once 'godie.php';
//To simplify this file, I put helper functions in external file, example: https://gist.github.com/npapratovic/3a75da43b147f55254bc6fd8503cf901
require_once 'helper-functions.php';
//lets load xml with articles. Example of this file is at the file bottom
$articles = 'Article.xml';
$article_arr = simplexml_load_file($articles);
//lets load xml with photo gallery image names. Example of this file is at the file bottom
$photos = 'PhotoGallery.xml';
$photo_arr = simplexml_load_file($photos);
/*
Important note:
Article.xml has field called "HasPhotogallery"; if value is "YES", then we need to check file PhotoGallery.xml and look for field called "ArticleID"; in this node are information about image which belongs to gallery added to this post. For example name of bigger image, thumb image etc.
*/
//Lets create array of posts and images
$posts = array();
foreach ($article_arr->Article as $Article) {
$posts[] = array(
'ID' => (string)$Article->ID, // ArticleTitle
'Title' => (string)$Article->Title, // ArticleTitle
'Excerpt' => (string)$Article->ShortDescription, // ArticleExcerpt
'Body' => (string)$Article->TextHtml, //ArticleBody
'Category' => (string)$Article->ArticleCategoryID, //ArticleCategoryID
'Publish' => (string)$Article->Publish, //Publish
'ArticleDate' => (string)$Article->ArticleDate, //ArticleDate
'LastChangeDate' => (string)$Article->LastChangeDate, //LastChangeDate
'MainImage' => (string)$Article->MainImage, //MainImage
'ThumbImage' => (string)$Article->ThumbImage, //ThumbImage
'HasPhotogallery' => (string)$Article->HasPhotogallery, //HasPhotogallery
);
}
$galleries = array();
foreach ($photo_arr->PhotoGallery as $PhotoGallery) {
$galleries[] = array(
'ImageID' => (string)$PhotoGallery->ImageID, // ImageID
'ArticleID' => (string)$PhotoGallery->ArticleID, // ArticleID
'Description' => (string)$PhotoGallery->Description, // Description
'ThumbImage' => (string)$PhotoGallery->ThumbImage, //ThumbImage
'NormalImage' => (string)$PhotoGallery->NormalImage, //NormalImage
'FullImage' => (string)$PhotoGallery->FullImage, //FullImage
'priority' => (string)$PhotoGallery->priority, //priority
);
}
if (!empty($posts)):
foreach ($posts as $single_post):
if ($single_post['Publish'] != 'YES') {
$post_status = 'draft';
} else {
$post_status = 'publish';
}
$published_date = mb_substr($single_post['ArticleDate'], 0, 10);
$published_time = substr($single_post['ArticleDate'], -8);
$published_date_time = $published_date.'-'.$published_time;
$modified_date = mb_substr($single_post['LastChangeDate'], 0, 10);
$modified_time = substr($single_post['LastChangeDate'], -8);
$modified_date_time = $modified_date.$modified_time;
$article_id = $single_post['ID'];
$main_image = $single_post['MainImage'];
$thumb_image = $single_post['ThumbImage'];
if ($main_image) {
$featured_image = $main_image;
} elseif ($thumb_image) {
$featured_image = $thumb_image;
} else {
$article_id = '0';
$featured_image = 'logo.png';
}
$image_url = 'img/' . $article_id . '/'. $featured_image;
$post = [
'post_title' => wp_strip_all_tags($single_post['Title']),
'post_excerpt' => wp_strip_all_tags($single_post['Excerpt']),
'post_content' => wp_strip_all_tags($single_post['Body']),
'post_date' => $published_date_time,
'post_date_gmt' => $published_date_time,
'post_modified' => $modified_date_time,
'post_modified_gmt' => $modified_date_time,
'post_status' => $post_status
];
//Create Post
$post_id = wp_insert_post($post, $wp_error);
//set post Categories
wp_set_object_terms($post_id, array($single_post['Category']), 'category');
//featured image:
create_featured_image($image_url, $post_id);
if ($single_post['HasPhotogallery'] == 'YES') {
foreach ($galleries as $single_gallery) {
if ($single_gallery['ArticleID'] == $article_id) {
$web_url = 'http://staging.mywebsite.com';
$gallery_image_url = '/multimedia/img/' . $article_id . '/'. $single_gallery['NormalImage'];
// Escape the url, just to be save
$image_url = esc_url($gallery_image_url);
// Cache info on the wp uploads dir
$wp_upload_dir = wp_upload_dir();
// get the file path
$path = parse_url($image_url, PHP_URL_PATH);
// File base name, e.g. image.jpg
$file_base_name = basename($image_url);
// Combine the two to get the uploaded file path
$uploaded_file_path = $web_url . $path;
$desc = $single_post['Title'];
//We are attaching uploaded image to this post. And returning image to to use it later when updateing galley field
$attach_id = media_sideload_image($uploaded_file_path, $post_id, $desc, 'id');
if (!is_wp_error($attach_id)) {
$array = get_field('field_5ea9492277b6f', $post_id, false);
if (!is_array($array)) {
$array = array();
}
$array[] = $attach_id;
update_field('field_5ea9492277b6f', $array, $post_id);
}
}
}
}
echo $single_post['Title'] . '' . ' imported <br />';
endforeach;
endif;
/*BONUS 1: example of Article.xml file*/
/*
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Article.xsd" generated="2020-04-27T15:25:46">
<Article>
<ID>01022011622064502</ID>
<ArticleDate>2011-02-01T00:00:00</ArticleDate>
<DateStart>2011-02-01T00:00:00</DateStart>
<ShortDescription>...ShortDescription...</ShortDescription>
<ThumbImage>images.jpg</ThumbImage>
<MainImage>IYFcro_444.jpeg</MainImage>
<Title>Article title</Title>
<TextHtml>...Article content....</TextHtml>
<ArticleCategoryID>Category name</ArticleCategoryID>
<UserID>dijana</UserID>
<Publish>YES</Publish>
<AltLink></AltLink>
<LastChangeDate>2013-08-30T11:48:03</LastChangeDate>
<ReadNumberOfTimes>2</ReadNumberOfTimes>
<ShowShortDescriptionWhenOpened>NO</ShowShortDescriptionWhenOpened>
<HasPhotogallery>NO</HasPhotogallery>
<CategoryMenuPriority>1</CategoryMenuPriority>
<MenuTitle>Menu Article title</MenuTitle>
</Article>
</dataroot>
*/
/*BONUS 2: example of PhotoGallery.xml file*/
/*
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="PhotoGallery.xsd" generated="2020-04-28T14:51:46">
<PhotoGallery>
<ImageID>1</ImageID>
<ArticleID>131020110318722</ArticleID>
<Description></Description>
<ThumbImage>IMG_1538.jpg</ThumbImage>
<NormalImage>IMG_1538.jpg</NormalImage>
<FullImage></FullImage>
<priority>1</priority>
</PhotoGallery>
<PhotoGallery>
<ImageID>2</ImageID>
<ArticleID>131020110318722</ArticleID>
<Description></Description>
<ThumbImage>IMG_1562.jpg</ThumbImage>
<NormalImage>IMG_1562.jpg</NormalImage>
<FullImage></FullImage>
<priority>2</priority>
</PhotoGallery>
<PhotoGallery>
<ImageID>3</ImageID>
<ArticleID>131020110318722</ArticleID>
<Description></Description>
<ThumbImage>IMG_1571.jpg</ThumbImage>
<NormalImage>IMG_1571.jpg</NormalImage>
<FullImage></FullImage>
<priority>3</priority>
</PhotoGallery>
<PhotoGallery>
<ImageID>4</ImageID>
<ArticleID>131020110318722</ArticleID>
<Description></Description>
<ThumbImage>IMG_5280.jpg</ThumbImage>
<NormalImage>IMG_5280.jpg</NormalImage>
<FullImage></FullImage>
<priority>4</priority>
</PhotoGallery>
</dataroot>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment