Importing Eleventy content into WordPress
Related to redoing this blog, I thought I’d quickly share how I exported all of my 11ty content and imported it into WordPress.
Steps
First up, I installed a fresh copy of WordPress and cleaned out the default posts, pages and comments.
Then, in the existing Eleventy project, I added the following page—which I named wp.njk
—which generates an XML file, which the WordPress importer can understand:
---
permalink: 'wp.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>YOUR NAME HERE</title>
<link>YOUR WEB ADDRESS</link>
<description>Just another WordPress site</description>
<pubDate>Wed, 15 Dec 2021 10:27:40 +0000</pubDate>
<language>en-GB</language>
<wp:wxr_version>1.2</wp:wxr_version>
<wp:base_site_url>YOUR WEB ADDRESS</wp:base_site_url>
<wp:base_blog_url>YOUR WEB ADDRESS</wp:base_blog_url>
<wp:category>
<wp:term_id>1</wp:term_id>
<wp:category_nicename><![CDATA[uncategorized]]></wp:category_nicename>
<wp:category_parent><![CDATA[]]></wp:category_parent>
<wp:cat_name><![CDATA[Uncategorized]]></wp:cat_name>
</wp:category>
<wp:term>
<wp:term_id>1</wp:term_id>
<wp:term_taxonomy><![CDATA[category]]></wp:term_taxonomy>
<wp:term_slug><![CDATA[uncategorized]]></wp:term_slug>
<wp:term_parent><![CDATA[]]></wp:term_parent>
<wp:term_name><![CDATA[Uncategorized]]></wp:term_name>
</wp:term>
<generator>https://wordpress.org/?v=5.8.1</generator>
{% for item in collections.items %}
<item>
<title><![CDATA[{{ item.date }}]]></title>
<link>YOUR WEB ADDRESS/{{ item.fileSlug }}/</link>
<pubDate>{{ item.data.title }}</pubDate>
<dc:creator><![CDATA[test]]></dc:creator>
<description></description>
<content:encoded><![CDATA[{{ item.templateContent | safe }}]]></content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id>{{ loop.index + 5 }}</wp:post_id>
<wp:post_date><![CDATA[{{ item.data.title | w3DateFilter }}]]></wp:post_date>
<wp:comment_status><![CDATA[closed]]></wp:comment_status>
<wp:ping_status><![CDATA[closed]]></wp:ping_status>
<wp:post_name><![CDATA[{{ item.fileSlug }}d]]></wp:post_name>
<wp:status><![CDATA[publish]]></wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type><![CDATA[post]]></wp:post_type>
<wp:post_password><![CDATA[]]></wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
<category domain="category" nicename="uncategorized"><![CDATA[Uncategorized]]></category>
</item>
{% endfor %}
</channel>
</rss>
The {% for item in collections.items %}
part refers to the Eleventy collection—which in my case was items
. I then loop through those to create a WordPress post. Make sure you replace YOUR WEB ADDRESS
with your actual website.
Then, import it into WordPress and bish, bash, bosh: you’re all done.
Back to blog