Create xml file to be used in wordpress post import

Posted by adedoy on Stack Overflow See other posts from Stack Overflow or by adedoy
Published on 2012-10-03T09:23:43Z Indexed on 2012/10/03 9:37 UTC
Read the original article Hit count: 268

Filed under:
|
|
|
|

Alright here is the thing, I have this site that was once wordpress but have been converted into 70+ static pages, the admin is deleted and the whole site is static(which means every page is in index.html), I want to create a script that makes an xml so that I will just have to import it in the new wordpress install.

So far, I am able to create an XML but it only imports one post.

The data source is the URL of a page and I use jquery $get to filter only to gather the post of a given archive.

//html
<input type="text" class="full_path">
<input type="button" value="Get Data" class="getdata">

//script

$('.getdata').click(function(){
            $.get($('.full_path').val(), function(data) {
              post = $(data).find('div [style*="width:530px;"]');
              $('.result').html(post.html());

            });
        });//get Data

Through AJAX I send the cleaned data into a php below that creates the XML:

$file = 'newpost.xml';
$post_data = $_REQUEST['post_data'];
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new post to the file
$catStr = '';
if(isset($post_data['categories']) && count($post_data['categories']) > 0){
    foreach($post_data['categories'] as $category) {
        $catStr .= '<category domain="category" nicename="'.$category.'"><![CDATA['.$category.']]></category>';
    }   
}
$tagStr = '';
if(isset($post_data['tags']) && count($post_data['tags']) > 0){
    foreach($post_data['tags'] as $tag) {
        $tagStr = '<category domain="post_tag" nicename="'.$tag.'"><![CDATA['.$tag.']]></category>';
    }
}
$post_name = str_replace(' ','-',$post_data["title"]);
$post_name = str_replace(array('"','/',':','.',',','[',']','“','”'),'',strtolower($post_name));

$post_date = '2011-4-0'.rand(1, 29).''.rand(1, 12).':'.rand(1, 59).':'.rand(1, 59);
$pubTime = rand(1, 12).':'.rand(1, 59).':'.rand(1, 59).' +0000';

$post = '
    <item>
        <title>'.$post_data["title"].'</title>
        <link>'.$post_data["link"].'</link>
        <pubDate>'.$post_data["date"].' '.$pubTime.'</pubDate>
        <dc:creator>admin</dc:creator>
        <guid isPermaLink="false">http://localhost/saunders/?p=1</guid>
        <description></description>
        <content:encoded><![CDATA['.$post_data["content"].']]></content:encoded>
        <excerpt:encoded><![CDATA[]]></excerpt:encoded>
        <wp:post_id>1</wp:post_id>
        <wp:post_date>'.$post_date.'</wp:post_date>
        <wp:post_date_gmt>'.$post_date.'</wp:post_date_gmt>
        <wp:comment_status>open</wp:comment_status>
        <wp:ping_status>open</wp:ping_status>
        <wp:post_name>'.$post_name.'</wp:post_name>
        <wp:status>publish</wp:status>
        <wp:post_parent>0</wp:post_parent>
        <wp:menu_order>0</wp:menu_order>
        <wp:post_type>post</wp:post_type>
        <wp:post_password></wp:post_password>
        <wp:is_sticky>0</wp:is_sticky>
        '.$catStr.'
        '.$tagStr.'
        <wp:postmeta>
            <wp:meta_key>_edit_last</wp:meta_key>
            <wp:meta_value><![CDATA[1]]></wp:meta_value>
        </wp:postmeta>
    </item>
';



// Write the contents back to the file with the appended post
file_put_contents($file, $current.$post);

After being appended I add the code below to complete the xml rss tag

</channel>
</rss>

If I look and compare the xml file of one that is exported from a wordpress site, I see little difference. Please HELP!!

here is a sample of a generated xml:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"
    xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:wp="http://wordpress.org/export/1.2/"
>
<channel>
    <title>lols why</title>
    <link>http://localhost/lols</link>
    <description>Just another WordPress site</description>
    <pubDate>Wed, 03 Oct 2012 04:24:04 +0000</pubDate>
    <language>en-US</language>
    <wp:wxr_version>1.2</wp:wxr_version>
    <wp:base_site_url>http://localhost/lols</wp:base_site_url>
    <wp:base_blog_url>http://localhost/lols</wp:base_blog_url>

    <wp:author><wp:author_id>1</wp:author_id><wp:author_login>adedoy</wp:author_login><wp:author_email>[email protected]</wp:author_email><wp:author_display_name><![CDATA[adedoy]]></wp:author_display_name><wp:author_first_name><![CDATA[]]></wp:author_first_name><wp:author_last_name><![CDATA[]]></wp:author_last_name></wp:author>


    <generator>http://wordpress.org/?v=3.4.1</generator>

    <item>
        <title>Sample lift?</title>
        <link>../../breast-lift/delaware-breast-surgery-do-i-need-a-breast-lift/</link>
        <pubDate>Wed, 03 Oct 2012 9:29:16 +0000</pubDate>
        <dc:creator>admin</dc:creator>
        <guid isPermaLink="false">http://localhost/lols/?p=1</guid>
        <description></description>
        <content:encoded><![CDATA[<p>sample</p>]]></content:encoded>
        <excerpt:encoded><![CDATA[]]></excerpt:encoded>
        <wp:post_id>1</wp:post_id>
        <wp:post_date>2011-4-0132:45:4</wp:post_date>
        <wp:post_date_gmt>2011-4-0132:45:4</wp:post_date_gmt>
        <wp:comment_status>open</wp:comment_status>
        <wp:ping_status>open</wp:ping_status>
        <wp:post_name>sample-lift?</wp:post_name>
        <wp:status>publish</wp:status>
        <wp:post_parent>0</wp:post_parent>
        <wp:menu_order>0</wp:menu_order>
        <wp:post_type>post</wp:post_type>
        <wp:post_password></wp:post_password>
        <wp:is_sticky>0</wp:is_sticky>
        <category domain="category" nicename="Sample Lift"><![CDATA[Sample Lift]]></category><category domain="category" nicename="Sample Procedures"><![CDATA[Yeah Procedures]]></category>
        <category domain="post_tag" nicename="delaware"><![CDATA[delaware]]></category>
        <wp:postmeta>
            <wp:meta_key>_edit_last</wp:meta_key>
            <wp:meta_value><![CDATA[1]]></wp:meta_value>
        </wp:postmeta>
    </item>
    <item>
        <title>lalalalalala</title>
        <link>../../administrative-tips-for-surgery/delaware-cosmetic-surgery-a-better-experience/</link>
        <pubDate>Wed, 03 Oct 2012 3:20:43 +0000</pubDate>
        <dc:creator>admin</dc:creator>
        <guid isPermaLink="false">http://localhost/lols/?p=1</guid>
        <description></description>
        <content:encoded><![CDATA[
                lalalalalala
            ]]></content:encoded>
        <excerpt:encoded><![CDATA[]]></excerpt:encoded>
        <wp:post_id>1</wp:post_id>
        <wp:post_date>2011-4-0124:39:30</wp:post_date>
        <wp:post_date_gmt>2011-4-0124:39:30</wp:post_date_gmt>
        <wp:comment_status>open</wp:comment_status>
        <wp:ping_status>open</wp:ping_status>
        <wp:post_name>lalalalalala</wp:post_name>
        <wp:status>publish</wp:status>
        <wp:post_parent>0</wp:post_parent>
        <wp:menu_order>0</wp:menu_order>
        <wp:post_type>post</wp:post_type>
        <wp:post_password></wp:post_password>
        <wp:is_sticky>0</wp:is_sticky>
        <category domain="category" nicename="lalalalalala"><![CDATA[lalalalalala]]></category>
        <category domain="post_tag" nicename="oink"><![CDATA[oink]]></category>
        <wp:postmeta>
            <wp:meta_key>_edit_last</wp:meta_key>
            <wp:meta_value><![CDATA[1]]></wp:meta_value>
        </wp:postmeta>
    </item>
</channel>
</rss>

Please tell me what am I missing....

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery