Skip to content

Instantly share code, notes, and snippets.

@qawemlilo
Forked from Jamesking56/WordPress.class.php
Created October 8, 2020 15:47
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 qawemlilo/e2dfffadb248a36afcfbd575bb416ac0 to your computer and use it in GitHub Desktop.
Save qawemlilo/e2dfffadb248a36afcfbd575bb416ac0 to your computer and use it in GitHub Desktop.
Read Wordpress Export XML to PHP
<?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;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment