Migrate WordPress posts to Contentful using the WP REST API and the Contentful JS SDK.
- Markdown: Post bodies are converted to Markdown to strip out inline styles and image dimensions.
- Assets: Featured and inline images will be transferred and their references in content replaced.
- Authors: Script looks for CF Person names that match WP User and reassign author attribution.
- Date Published: Preserved
- Prerequisites
yarn install
yarn start
Make sure to temporarily disable any publish webhooks (e.g. Netlify) because this is gonna do a lot of publishing.
- Yarn
- Contentful CLI (Logged in)
Assumes the following content types are already created in Contentful:
For best results, create Persons with matching names so posts authors in the two systems can be matched up and attributions preserved. Otherwise, the fallback Person ID will be used.
See fields in source
Contentful CMA tokens have access to all environments for migration purposes so make sure to double-check the relevant config var to see which is being targeted.
The /wp-json/wp/v2/users
should return a paginated list of users, if it does not and returns an error like the one below (via Wordfence) it will need to be enabled before the author tasks can run effectively:
{
"code": "rest_user_cannot_view",
"message": "Sorry, you are not allowed to list users.",
"data": {
"status": 401
}
}
Values required for all of the variables below, or the test config (first) task will throw.
# Base URL for API requests
WP_API_URL="https://website.com/wp-json/wp/v2"
# WP returns absolute URLS, we use relative
REDIRECT_BASE_URL="https://website.com"
# Generated in web app interface
CONTENTFUL_CMA_TOKEN="[token]"
# Go to Project's Settings > General Settings
CONTENTFUL_SPACE_ID="[space-id]"
# Sandbox environment name, or master
CONTENTFUL_ENV_NAME="[staging]"
# Default locale (in my case "en-US")
CONTENTFUL_LOCALE="[locale]"
# "Person" ID used when a match cant be found
# comparing WP User to CF Person names (post
# author attribution)
CONTENTFUL_FALLBACK_USER_ID="[id]"
Lots of inspiration from this article and linked gists, it inspired the general approach and some of the of code is used directly.
- Replace Listr with Ink static & dynamic logging
- Optional, incremental steps
- Multi-phase approach
- Migrate content to sandbox env
- Export structure/content using CF Migration CLI
- Use CF tooling to migrate content into master