[PYTHON] Migrate your own CMS data to WordPress

I checked it because I needed to do it at work, so I made a note.

The destination WordPress used a custom post type and ACF, so it was a bit annoying.

Rough flow

I decided to create WordPress XML, a WordPress import tool, from my own CMS data and migrate the data.

As you can see by writing the WordPress site using ACF to XML and looking inside, ACF seems to have an identifier like field_5671e74fc8c5e assigned to each field, so this is necessary when creating WordPress XML. So, the setup is as follows.

  1. Set custom post type and custom field in the destination WordPress.
  2. In the destination WordPress, register the sample data with the custom field values entered properly.
  3. Export XML in the destination WordPress. Look at its contents to see the custom post type and custom field identifiers.
  4. Convert the data of the migration source's own CMS to XML for WordPress import tool. Images should be accessible by URL.
  5. Import the created XML in the destination WordPress.

XML for WordPress import tool

Rough structure

The rough structure of XML is as follows.

WordPressインポートツール用のXMLの構造

ʻAuthor, category, and term` correspond to the WordPress users, categories, and terms.

ʻItem contains not only articles and pages, but also images and ACF custom fields in ʻitem.

The value of the custom field registered in each article is in postmeta.

An ID is assigned to ʻauthor, category, term, and ʻitem, and they are used for parent-child relationships and for specifying images defined in item.

By the way, ʻauthor` seems to be able to be imported without it.

image

The image is registered as ʻitem`.

The URL of the image is defined as a child element named ʻattachment_url`.

If you register ʻitem` that represents an article as a parent element, it will be imported properly as an image associated with that article.

The custom field of the image contains the ID of the image defined in item.

Custom field data

The data of the custom field is in postmeta, but when there is a text field named" year ", it looks like this.

<wp:postmeta>
    <wp:meta_key><![CDATA[year]]></wp:meta_key>
    <wp:meta_value><![CDATA[2015]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
    <wp:meta_key><![CDATA[_year]]></wp:meta_key>
    <wp:meta_value><![CDATA[field_5670f2bfd2c5a]]></wp:meta_value>
</wp:postmeta>

In order to create XML for import, you need to know year and field_5670f2bfd2c5a.

Repeat field

For example, if there is a repeating field called ʻimages, and there are multiple images called ʻimage and a text field called caption in it, it will be postmeta as shown below. The 2 on the third line seems to contain the defined number of elements.

<wp:postmeta>
    <wp:meta_key><![CDATA[images]]></wp:meta_key>
    <wp:meta_value><![CDATA[2]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
    <wp:meta_key><![CDATA[_images]]></wp:meta_key>
    <wp:meta_value><![CDATA[field_5670f0d3391f8]]></wp:meta_value>
</wp:postmeta>

<!--First image-->
<wp:postmeta>
    <wp:meta_key><![CDATA[images_0_image]]></wp:meta_key>
    <wp:meta_value><![CDATA[10]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
    <wp:meta_key><![CDATA[_images_0_image]]></wp:meta_key>
    <wp:meta_value><![CDATA[field_5670f2bad2c5a]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
    <wp:meta_key><![CDATA[images_0_caption]]></wp:meta_key>
    <wp:meta_value><![CDATA[Image 1]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
    <wp:meta_key><![CDATA[_images_0_caption]]></wp:meta_key>
    <wp:meta_value><![CDATA[field_5670f2bfr2c5a]]></wp:meta_value>
</wp:postmeta>

<!--Second image-->
<wp:postmeta>
    <wp:meta_key><![CDATA[images_1_image]]></wp:meta_key>
    <wp:meta_value><![CDATA[11]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
    <wp:meta_key><![CDATA[_images_1_image]]></wp:meta_key>
    <wp:meta_value><![CDATA[field_5670f2bad2c5a]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
    <wp:meta_key><![CDATA[images_1_caption]]></wp:meta_key>
    <wp:meta_value><![CDATA[Image 2]]></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
    <wp:meta_key><![CDATA[_images_1_caption]]></wp:meta_key>
    <wp:meta_value><![CDATA[field_5670f2bfr2c5a]]></wp:meta_value>
</wp:postmeta>

When the generated XML is not imported successfully

You can see the import process by looking at the * WordPress Import Tool * plugin code. Since there are only 2 files, I think it's faster to try putting debug code in the code of the * WordPress import tool * plugin than trying hard by trial and error of XML.

By the way, the import process is roughly like the following flow.

  1. Parse the XML and divide it into ʻauthors, category, term, and ʻitem.
  2. Import each
  3. Associate the parent-child relationship of ʻitem` (association of articles with images, etc.)

ʻItem (probably category`) controls not to import the same thing by looking at the ID, so if you do not properly squeeze the ID, it will not be imported for some reason. .. (hooked on.)

WordPress XML generation library for Python

I've posted a Python script I made for work on GitHub, so if you think you can use it, please use it.

wordpress_importer_util

I'm using arrow for processing around the date, so just put this in with pip install arrow.

ʻExamples / my_wordpress.py` is an example of a Python script that actually generates XML.

I haven't tried category and term because I didn't actually use them. Please use at your own risk.

Recommended Posts

Migrate your own CMS data to WordPress
Annotate your own data to train Mask R-CNN
Migrate Qiita articles to WordPress
How to create your own Transform
Bridge ROS to your own protocol
Add your own content view to mitmproxy
To import your own module with jupyter
How to install your own (root) CA
Dump SQLite3 data and migrate to MySQL
How to access data with object ['key'] for your own Python class
Try to make your own AWS-SDK with bash
How to define your own target in Sage
[For recording] Keras image system Part 1: How to create your own data set?
Train Stanford NER Tagger with your own data
[AWS] Migrate data from DynamoDB to Aurora MySQL
Steps to install your own library with pip
Memo to create your own Box with Pepper's Python
Create your own Big Data in Python for validation
[Introduction to Udemy Python 3 + Application] 66. Creating your own exceptions
Try to improve your own intro quiz in Python
Wagtail Recommendation (5) Let's add your own block to StreamField
Try to put LED in your own PC (slightly)
[Road to intermediate Python] Define in in your own class
Create your own exception
Use gokart to test pandas associations on your data pipeline.
[Ansible] How to call variables when creating your own module
How to use pyenv and pyenv-virtualenv in your own way
(Note) How to pass the path of your own module
How to get an overview of your data in Pandas
Try HeloWorld in your own language (with How to & code)