Created
September 17, 2012 05:37
-
-
Save doginthehat/3735729 to your computer and use it in GitHub Desktop.
Import tab indented sitemap into wordpress
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
define("LIPSUM","bacon"); | |
$site = <<<EOF | |
Top Page | |
Sub Page 1 | |
Sub Page 2 | |
Sub Page 3 | |
Top Page 2 | |
Top Page 3 | |
Top Page 4 | |
EOF; | |
$site = preg_split('/\n/', $site); | |
var_dump($site); | |
$pages = array(); | |
class node { | |
var $title; | |
var $id = 0; | |
var $sub = array(); | |
public function __construct($title) | |
{ | |
$this->title = $title; | |
} | |
} | |
function parseSite(&$site, &$target, $level = 0) | |
{ | |
$node = null; | |
while($site) | |
{ | |
$item = $site[0]; | |
if(preg_match('/^\t+/', $item,$tmp)) | |
$indent = strlen($tmp[0]); | |
else | |
$indent = 0; | |
if ($indent == $level) | |
{ | |
$node = new node(trim($item)); | |
$target[] = $node; | |
array_shift($site); | |
} | |
elseif ($indent > $level) | |
{ | |
if ($indent > $level+1) | |
die ("{$item}: nesting too fast, one tab at a time"); | |
if (!$node) | |
die ("{$item}: nesting issue, no node to nest into"); | |
parseSite($site, $node->sub,$level+1); | |
} | |
else // $indent < $level | |
{ | |
return; | |
} | |
} | |
} | |
function addPages($pages, $content = true, $parent = 0) | |
{ | |
$menu_order = 10; | |
foreach($pages as $page) | |
{ | |
$wp_page = get_page_by_title($page->title); | |
if ($wp_page) | |
{ | |
$page->ID = $wp_page->ID; | |
$page->ID = $wp_page->ID; | |
$data = array( | |
'ID' => $wp_page->ID, | |
'menu_order'=>$menu_order, | |
'post_parent'=>$parent, | |
); | |
wp_update_post($data); | |
} | |
else | |
{ | |
$data = array( | |
'menu_order'=>$menu_order, | |
'post_status'=>'publish', | |
'post_type'=>'page', | |
'post_parent'=>$parent, | |
'post_title'=>$page->title | |
); | |
if ($content) | |
{ | |
if ($content === true) | |
$data['post_content'] = getLipsum(); | |
else | |
$data['post_content'] = $content; | |
} | |
$page_id = wp_insert_post( $data ); | |
if ($page_id === 0) | |
{ | |
die("oh crap, can't insert post data"); | |
} | |
else | |
{ | |
$page->ID = $page_id; | |
} | |
} | |
if (count($page->sub)>0) | |
addPages($page->sub,$content, $page->ID); | |
$menu_order+= 10; | |
} | |
} | |
function getLipsum() | |
{ | |
switch(LIPSUM) | |
{ | |
case 'bacon': | |
return implode("\n\n",json_decode(file_get_contents('https://baconipsum.com/api/?type=all-meat¶s=6&start-with-lorem=0'))); | |
break; | |
default: | |
return file_get_contents('http://loripsum.net/api/long/plaintext/decorate'); | |
} | |
} | |
parseSite($site, $pages); | |
addPages($pages); | |
echo 'All done!'; | |
exit; |
Also, watch out, it's particularly not tolerant to duplicate page titles!
Now with bacon
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is fairly basic. Best to run it at early stages of a website build.
Needs access to the WP functions so place it somewhere where it'll work.
Use at your own risk.