action.skip

Importing posts from WordPress

Replacing Wordpress is one of the most common use cases for Headless CMS, and for good reasons. Let's take a look at how easy it's going to be.

Hint

Instead of coding, you can try our one-click migration tool to move your data from WordPress to Flotiq. All you need is your personal Flotiq Read And Write API Key and your WordPress Site address. Read more in our blog post.

Export Wordpress data

Go to Tools → Export in the main Wordpress menu

in the Export view - accept the defaults and press the Download Export File button

save the file and remember the path.

Create a BlogPost Content Type in Flotiq

You now have to create a Content Type Definition in Flotiq, and this is how you describe the kind of content you will be storing in the system. For a simple blog, you can use the template that we prepared, or go to the Content Type Editor and create a Content Type like the following:

Download the SDK

For this tutorial, I will use the Python SDK, but you can easily adapt this to your favorite language (or reach out to us and ask for help!). You can download the SDK from Flotiq dashboard, once it's built and downloaded - extract it and head to the extracted folder. Make sure you have all the required python packages installed by running

pip install -r requirements.txt

and then create a new file, called import.py in the extracted folder.

Hint

Remember, the Flotiq API docs and SDKs are automatically generated for you, based on your own Content Types. That means you have to download the SDK after you created the BlogPosts content type.

Connect to Flotiq API

Now, let's connect to your Flotiq API, enter the following lines into the import.py file:

import flotiq
from flotiq.rest import ApiException
import sys

configuration = flotiq.Configuration()
configuration.api_key['X-AUTH-TOKEN'] = sys.argv[1]
api_instance = flotiq.ContentBlogPostsApi(flotiq.ApiClient(configuration))

entries = api_instance.entries_list()
print(entries)

Hint

For the next steps you will need your API key, go to your user profile view and copy the Read/Write key from there.

Now let's try to execute this script and see if the connection is up and running, remember to put your API key in the command line:

python import.py YOUR_API_KEY
{'data': []}

unless you already added a blog entry - you will see an empty response. That was easy, right?

Success

You made your first call to the Flotiq API. That's great! Keep reading, we're almost done!

Import XML

In this step we will modify the code, it will now:

  • read through the WordPress XML dump
  • create a BlogPosts object for each wp:post item in the dump
  • call the create method and import each post through your Flotiq API.

Make sure you have the lxml python package, it's the one we're going to use to parse through the XML tree:

pip install lxml

add this to your import.py file

tree = ET.parse(sys.argv[2])
root = tree.getroot()

namespaces = {'wp':'wp', 'content':'content', 'excerpt':'excerpt'}
items = root.findall(".//wp:post_type",namespaces)
print("Loaded XML, number of posts:" + str(len(items)))

and verify if it correctly reads the XML (remember to point to the correct location of the WP dump file):

python import.py YOUR_API_KEY wordpress-export.xml
Loaded XML, number of posts:20172
...

Now, for each post - create an appropriate blog entry and save it into Flotiq:

import uuid

for item in items:
    post = item.getparent()
    title = post.find('title').text
    content = post.find('content:encoded', namespaces).text
    date = post.find('wp:post_date', namespaces).text
    link = post.find('link').text
    slug = post.find('wp:post_name', namespaces).text
    try:
        post = flotiq.models.Blogposts(
            id=str(uuid.uuid1()), 
            slug= slug,
            title=title,
            publish_date=date,
            content=content)
        api_instance.createblogposts(blogposts=post)
    except Exception as e:
        print(e)

this is the final listing of your program

import flotiq
from flotiq.rest import ApiException
import uuid
import sys
from lxml import etree as ET

tree = ET.parse(sys.argv[2])
root = tree.getroot()

namespaces = {'wp':'wp', 'content':'content', 'excerpt':'excerpt'}
items = root.findall(".//wp:post_type",namespaces)

configuration = flotiq.Configuration()
configuration.api_key['X-AUTH-TOKEN'] = sys.argv[1]
api_instance = flotiq.ContentBlogPostsApi(flotiq.ApiClient(configuration))

print("Loaded XML, number of posts:" + str(len(items)))

for item in items:
    post = item.getparent()
    title = post.find('title').text
    content = post.find('content:encoded', namespaces).text
    date = post.find('wp:post_date', namespaces).text
    link = post.find('link').text
    slug = post.find('wp:post_name', namespaces).text
    try:
        post = flotiq.models.Blogposts(
            id=str(uuid.uuid1()), 
            slug= slug,
            title=title,
            publish_date=date,
            content=content)
        api_instance.createblogposts(blogposts=post)
    except Exception as e:
        print(e)

Please mind that we only cared to import the basic fields, like slug, title, content, etc. Feel free to amend this code and import anything else you might need.

Success

Hopefully, you now have a bunch of blog posts imported from Wordpress. Go ahead and fork our example Gatsby starter and deploy it to Heroku - if you followed our naming closely - you will only need to provide your Read Only API key to have a fully working blog page in a couple of minutes!