Last active
April 19, 2021 12:05
-
-
Save Jamesking56/4773838 to your computer and use it in GitHub Desktop.
Read Wordpress Export XML to PHP
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 | |
/** | |
* WordPress class - Manages the WordPress XML file and gets all data from that. | |
*/ | |
class Wordpress | |
{ | |
public static $wpXML; | |
function __construct($wpXML) | |
{ | |
$this->wpXML = $wpXML; | |
} | |
public function getPosts() | |
{ | |
$xml = simplexml_load_file($this->wpXML); | |
$posts = array(); | |
foreach($xml->channel->item as $item) | |
{ | |
$categories = array(); | |
foreach($item->category as $category) | |
{ | |
//echo $category['domain']; | |
if($category['nicename'] != "uncategorized" && $category['domain'] == "category") | |
{ | |
//echo 'Yep'; | |
$categories[] = $category['nicename']; | |
} | |
} | |
$content = $item->children('http://purl.org/rss/1.0/modules/content/'); | |
$posts[] = array( | |
"title"=>$item->title, | |
"content"=>$content->encoded, | |
"pubDate"=>$item->pubDate, | |
"categories"=>implode(",", $categories), | |
"slug"=>str_replace("/", "", str_replace("http://blog.jamesking56.co.uk/", "", $item->guid)) | |
); | |
} | |
return $posts; | |
} | |
} | |
?> |
Really cool function, thanks a lot!
Anybody else getting these errors?
simplexml_load_file(): I/O warning : failed to load external entity "<?xml version="1.0" encoding="UTF-8" ?>
Anybody else getting these errors?
simplexml_load_file(): I/O warning : failed to load external entity "<?xml version="1.0" encoding="UTF-8" ?>
See this StackOverflow answer: https://stackoverflow.com/a/21661663/941446
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@cristianfx I am amazed this still works in 2019. Metadata could probably be parsed in a similar way.