php - 创建要在 wordpress 后导入中使用的 xml 文件

标签 php jquery xml ajax wordpress

好吧,我的网站曾经是曾经是 wordpress,但现在已经转换成 70 多个静态页面,管理员被删除并且整个站点是静态的(这意味着每个页面都在 index.html 中),我想创建一个生成 xml 的脚本,这样我只需要将它导入到新的 wordpress 安装中。

到目前为止,我能够创建 XML,但它仅导入一篇文章

数据源是页面的 URL,我使用 jquery $get 进行过滤以仅收集给定存档的帖子。

//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

通过 AJAX,我将清理后的数据发送到下面创建 XML 的 php:

$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){
    //populate post_tag like the above
}
$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);

追加后我添加下面的代码来完成 xml rss 标签

</channel>
</rss>

如果我查看并比较从 wordpress 站点导出的 xml 文件,我发现差别不大。请帮助!!

这是生成的 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>wazzup@gmail.com</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>

请告诉我我错过了什么....

最佳答案

好吧,重复一遍并确保我理解你的意思,这是从你的静态文件返回到 wordpress 的尝试。

要做到这一点,关于 importing from an RSS feed 的 WP 法典实际上概述了三种主要方法。 , (X)HTML , 和 static pages .这就是说,如果您有格式良好的 HTML,最简单的方法之一就是使用 HTML Import 2插件,在法典中引用。它为您提供了许多选项,可以从目录、URL 执行此操作,并从新安装的管理屏幕指定搜索参数。

如果这对您不起作用,或者您真的想将所有页面转换为 XML 以便导入,因为您已经在使用 PHP 生成 XML,我强烈建议您使用 PHP DOM用于从静态文件中打开、解析、提取和写入数据的库。这将使您免于浏览整个站点以生成 XML 所花费的时间,并且它会一次性生成所有页面的完整 XML 文件。

无论哪种方式,我都建议使用普通的 RSS 文件,而不是尝试重新创建 WordPress 的自定义格式,因为如果操作得当,它会为所有内容类型提供大量数据库引用( map ID 等)。由于您似乎不需要它,请尝试制作一个普通的 RSS 文件,该文件包含您所需的最少内容,并且您可以编辑这些帖子以重新分配作者等。如果需要,在使用 codex 导入后importing from an RSS feed 的说明.这会容易得多。

关于php - 创建要在 wordpress 后导入中使用的 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12705473/

相关文章:

c# - 通过 XSLT 在 XML 中格式化日期

c++ - 遍历不包含标签名称的 PTree 子项

php - `GitHub Pages` 是否允许 HTTP "Post"方法?

php - 在文本框中从数据库中搜索数据以显示 - Laravel

jquery - Kendo 自动完成如何禁用默认的 css 样式

javascript - 在 IE 中显示密码字段的占位符文本

jquery - 如何检测Html元素是否在View中?

javascript - 创建动态文本框然后写入文本文件

php - 使用 prepare 命令更新我的表,将值增加 2

java - 如何在 JavaScript 中读取 XML 服务器响应?