Skip to content

Instantly share code, notes, and snippets.

@kfriend
Created July 27, 2017 18:35
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 kfriend/38d93627f3d1c4fb99b21d03aeb115e5 to your computer and use it in GitHub Desktop.
Save kfriend/38d93627f3d1c4fb99b21d03aeb115e5 to your computer and use it in GitHub Desktop.
Example ExpressionEngine to Wordpress Import Format
<?php $authors = array() ?>
<?= '<?' ?>xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:excerpt="http://wordpress.org/export/1.0/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.0/"
>
<channel>
<title>Import</title>
<link>http://websitedomain.com</link>
<description></description>
<pubDate></pubDate>
<generator>http://wordpress.org/?v=2.8.4</generator>
<language>en-US</language>
<wp:wxr_version>1.0</wp:wxr_version>
<wp:base_site_url>http://websitedomain.com</wp:base_site_url>
<wp:base_blog_url>http://websitedomain.com</wp:base_blog_url>
<?php $allowedTypes = array('jpg', 'jpeg', 'gif', 'png') ?>
{exp:file:entries directory_id="1|2|3" dynamic="no"}
<?php if (in_array(strtolower(pathinfo('{filename}', PATHINFO_EXTENSION)), $allowedTypes)): ?>
<item>
<title><?= pathinfo('{title}', PATHINFO_FILENAME) ?></title>
<link></link>
<pubDate>{upload_date format="{DATE_RSS}"}</pubDate>
<dc:creator></dc:creator>
<guid isPermaLink="false">{file_url}</guid>
<description>{description}</description>
<content:encoded><![CDATA[]]></content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id>{file_id}</wp:post_id>
<wp:post_date>{upload_date format="{DATE_RSS}"}</wp:post_date>
<wp:post_date_gmt>{upload_date format="{DATE_RSS}"}</wp:post_date_gmt>
<wp:comment_status><![CDATA[closed]]></wp:comment_status>
<wp:ping_status><![CDATA[closed]]></wp:ping_status>
<wp:post_name><![CDATA[{file_name}]]></wp:post_name>
<wp:status><![CDATA[inherit]]></wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type><![CDATA[attachment]]></wp:post_type>
<wp:post_password><![CDATA[]]></wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
<wp:attachment_url><![CDATA[{file_url}]]></wp:attachment_url>
</item>
<?php endif ?>
{/exp:file:entries}
{exp:channel:entries channel="blog" limit="9999" sort="asc"}
<item>
<?php
$authors[] = '{author_id}';
?>
<title><?= e('{title}') ?></title>
<link></link>
<pubDate>{entry_date format="{DATE_RSS}"}</pubDate>
<dc:creator><![CDATA[{author}]]></dc:creator>
<guid isPermaLink="false"></guid>
<description></description>
<content:encoded><![CDATA[{post_content}]]></content:encoded>
<excerpt:encoded><![CDATA[<?= strip_tags('{post_summary}') ?>]]></excerpt:encoded>
<wp:post_id></wp:post_id>
<wp:post_date>{entry_date format="{DATE_RSS}"}</wp:post_date>
<wp:post_date_gmt>{entry_date format="{DATE_RSS}"}</wp:post_date_gmt>
<wp:comment_status>closed</wp:comment_status>
<wp:ping_status>closed</wp:ping_status>
<wp:post_name>{url_title}</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>
{categories}
<category><![CDATA[{category_name}]]></category>
<category domain="category" nicename="{category_url_title}"><![CDATA[{category_name}]]></category>
<category domain="category" nicename="uncategorized"><![CDATA[Uncategorized]]></category>
{/categories}
<wp:postmeta>
<wp:meta_key><![CDATA[_yoast_wpseo_title]]></wp:meta_key>
<wp:meta_value><![CDATA[<?= e(trim(<<<EOS
'{post_document_title}'
EOS
)) ?>]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
<wp:meta_key><![CDATA[_yoast_wpseo_metadesc]]></wp:meta_key>
<wp:meta_value><![CDATA[<?= e(trim(<<<EOS
'{post_document_title}'
EOS
)) ?>]]></wp:meta_value>
</wp:postmeta>
<?php
$imgAttrs = str_first_img_attrs(<<<EOS
{post_content}
EOS
);
if (!empty($imgAttrs['src'])) {
$img = get_img($imgAttrs['src']);
}
?>
<?php if (!empty($img)): ?>
<wp:postmeta>
<wp:meta_key><![CDATA[_thumbnail_id]]></wp:meta_key>
<wp:meta_value><![CDATA[<?= e($img->file_id) ?>]]></wp:meta_value>
</wp:postmeta>
<?php endif ?>
</item>
{/exp:channel:entries}
<?php foreach ($authors as $authorId): ?>
<?php
$author = ee()->db->where('member_id =', $authorId)->get('exp_members')->first_row();
$nameParts = explode(' ', $author->screen_name);
?>
<wp:author>
<wp:author_id><?= $author->member_id ?></wp:author_id>
<wp:author_login>
<![CDATA[<?= $author->username ?>]]>
</wp:author_login>
<wp:author_email>
<![CDATA[<?= $author->email ?>]]>
</wp:author_email>
<wp:author_display_name>
<![CDATA[<?= $author->screen_name ?>]]>
</wp:author_display_name>
<wp:author_first_name>
<![CDATA[<?= $nameParts[0] ?>]]>
</wp:author_first_name>
<wp:author_last_name>
<![CDATA[<?= !empty($nameParts[1]) ? $nameParts[1] : '' ?>]]>
</wp:author_last_name>
</wp:author>
<?php endforeach ?>
</channel>
</rss>
<?php
function e($value) {
return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
}
function str_first_img_attrs($string) {
if (!$string) {
return null;
}
preg_match('/<img\s+([^>]*)>/i', $string, $matches);
if (empty($matches[1]) or trim($matches[1]) === '') {
return null;
}
// Libxml throws Exceptions AND spits out errors... need to handle both situations
// Current setting
$errorSetting = libxml_use_internal_errors();
// Disable errors
libxml_use_internal_errors(true);
try {
// Remove self-closing marker
$attrs = preg_replace('#\s*\/\s*$#', '', $matches[1]);
// Parse the attributes. We need to manually construct the <img> tag as self-closing,
// or SimpleXMLElement will fail to parse it.
$xml = new SimpleXMLElement("<img {$attrs} />");
// Reset error setting
libxml_use_internal_errors($errorSetting);
if (libxml_get_errors())
{
return null;
}
// Need to use current() because SimpleXMlElement is horribly designed in this regard:
// http://stackoverflow.com/questions/11439829/simplexml-attributes-to-array#answer-13677624
return current($xml->attributes());
} catch (Exception $exception) {
// Reset error setting
libxml_use_internal_errors($errorSetting);
return null;
}
}
function get_img($path) {
$path = parse_url($path);
$path = $path['path'];
$query = get_instance()->db
->like('rel_path', $path)
->limit(1)
->get('exp_files');
if (!$query->num_rows()) {
return null;
}
$return = $query->first_row();
$query->free_result();
return $return;
}
// Closing tag required by EE
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment