Skip to content

Migrate WordPress posts to Contentful using the WP REST API and the Contentful JS SDK.

License

Notifications You must be signed in to change notification settings

uswitch/wordpress-to-contentful

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WordPress to Contentful

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

🚀 Usage

  1. Prerequisites
  2. yarn install
  3. yarn start

⚠️ Prerequisites

Webhooks

Make sure to temporarily disable any publish webhooks (e.g. Netlify) because this is gonna do a lot of publishing.

Globally installed

Contentful Content Types

Assumes the following content types are already created in Contentful:

Person: person

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.

Blog Post: blogPost

See fields in source

Contentful Content Management API

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.

WP Users API route

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
  }
}

⚙️ Config

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]"

🙏 Cite

Lots of inspiration from this article and linked gists, it inspired the general approach and some of the of code is used directly.


☑️ Goals

  • Replace Listr with Ink static & dynamic logging
  • Optional, incremental steps
  • Multi-phase approach
    1. Migrate content to sandbox env
    2. Export structure/content using CF Migration CLI
    3. Use CF tooling to migrate content into master

About

Migrate WordPress posts to Contentful using the WP REST API and the Contentful JS SDK.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%