Created
December 21, 2022 18:48
-
-
Save chaddupuis/120f2253876abdb3bf6e6138f93e3ac0 to your computer and use it in GitHub Desktop.
Wordpress XML Dump w/Images and WP Rest Api - Content Migration to Processwire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace ProcessWire; | |
// Uses a bootstrapped processwire site from a php script to read in | |
// both an xml export with images and talk to existing live wp site | |
// via Rest API (json output) to move all content and images over to new site. | |
require('/your/location/wire/core/ProcessWire.php'); | |
$bl = new ProcessWire('/your/location/', 'https://your-site.com/'); | |
// Read WP XML Export (with images from plugin) | |
$myposts = simplexml_load_file('your-wp-xml-dump.xml'); | |
$myposts->registerXPathNamespace ('wp', 'http://wordpress.org/export/1.0/'); | |
$pointer = 0; | |
foreach ($myposts->channel->item as $mypost) { | |
$pid = $mypost->children('wp', true)->post_id; | |
$ptitle = $mypost->title; | |
$purl = basename($mypost->link); | |
$pdate = strtotime($mypost->pubDate); | |
$puser = $mypost->children('dc', true)->creator; | |
$puser = strtolower($puser); | |
$pfocus = ''; | |
foreach($mypost->category as $tagit) { | |
if($tagit['domain'] == 'category') { | |
$pfocus = $tagit; | |
} | |
if($tagit['domain'] == 'post_tag') { | |
$ptype = $tagit; | |
} | |
} | |
$pbody = $mypost->children('content', true)->encoded; | |
// Update internal links | |
// https://site.com/*/title to /posts/title | |
$oldurlcats = array("https://wp-site.com/cat1/", "https://wp-site.com/cat2/", "https://wp-site.com/cat3/", "https://wp-site.com/cat4/", "https://wp-site.com/cat5/", "https://wp-site.com/cat5/", "https://wp-site.com/cat6/"); | |
$pbodyupdlinks = str_replace($oldurlcats,'https://wp-site.com/posts/',$pbody); | |
// remove <span style=\"font-weight: 400\;\"> | |
$pbodycleanfonts = preg_replace('/<(span)[^\>]+>(.*?)<\/\1>/i', '\2', $pbodyupdlinks); | |
// Clean up the body img tags (float right) and links | |
$imgpattern = '/(<img\s+).*?src=((\".*?\")|(\'.*?\')|([^\s]*)).*\/>/i'; | |
$imgreplacement = '<img class="img-fluid img-thumbnail float-right" src=$2>'; | |
$pbodycleanimg = preg_replace($imgpattern, $imgreplacement, $pbodycleanfonts); | |
$pbodycleanimg = str_replace('src="/wp-content/', 'src="https://wp-site.com/wp-content/', $pbodycleanimg); | |
// end of pbody manipulations | |
$pbodyfinal = $pbodycleanimg; | |
// images | |
// json pull like https://wp-site.com/wp-json/wp/v2/media/2319 | |
// https://wp-site.com/wp-json/wp/v2/posts/2392 | |
$posturl = 'https://wp-site.com/wp-json/wp/v2/posts/'.$pid; | |
$postcontent = file_get_contents($posturl); | |
$postjson = json_decode($postcontent, true); | |
$postfeaturedimg = ''; | |
$postfeaturedimg = $postjson['featured_media']; | |
// If there is a featuredimage find the url of it | |
if (!empty($postfeaturedimg)) { | |
$imgurl = 'https://wp-site.com/wp-json/wp/v2/media/'.$postfeaturedimg; | |
$imgcontent = file_get_contents($imgurl); | |
$imgjson = json_decode($imgcontent, true); | |
$postfeaturedimgurl = $imgjson['guid']['rendered']; | |
} else { $postfeaturedimgurl = ''; echo 'NO IMAGE SO DEFAULT WILL BE USED'.PHP_EOL; } | |
// Turn this off to test before writing | |
$actuallywrite = 1; | |
if ($actuallywrite==1) { | |
// Create the Files | |
$p = new Page(); // create new page object | |
$p->template = 'page-blog'; // set template | |
$p->parent = wire('pages')->get('/posts/'); // set the parent | |
$p->name = $purl; // give it a name used in the url for the page | |
$p->title = $ptitle; // set page title (not neccessary but recommended) | |
$p->body = $pbodyfinal; | |
$p->post_created_date = date($pdate); // might need to tweak | |
$p->save(); | |
$pbuser = $bl->wire('users')->get($puser); | |
$p->created_users_id = $pbuser->id; | |
$p->save(array('quiet' => true)); | |
$focuses = $bl->wire('pages')->get("template=category-articlefocus, title=$pfocus"); | |
$p->category_article_focus = $focuses->id; | |
$types = $bl->wire('pages')->get("template=category-articletype, title=$ptype"); | |
$p->category_article_type = $types->id; | |
$p->save(); | |
if (!empty($postfeaturedimg)) { | |
$p->featured_image = $postfeaturedimgurl; | |
$p->save(); | |
} | |
// final save to get it to get remote images | |
$p->save(); | |
} // END ACTUALLYWRITE | |
$pointer++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment