This is forked from https://github.com/jonashcroft/wp-to-contentful.
Basic WP to Contentful migration
This only migrates:
- Post Title
- Slug
- Post Content and images in the contents
- Featured Images
Still does not detect if the existing post is already importer and does not continue from where the import last stopped when the stript is re-run if it encounters an error mid-import.
This script will run in the terminal via Node. You need to have npm installed.
-
Clone the repository
git clone git@github.com/TheCodeCompany/tcc-wp-to-contentful cd wp-to-contentful -
Install dependencies
npm install
-
Set up configuration
npm run setup
-
Edit your configuration in relation to WP & Contentful
In your Contentful admin panel:
- Go to Settings → API keys
- Create or select a Content Management API key
- Copy the Space ID and Content Management Token``
- Open
config.jsand update with:
a. WordPress:
- endpoint - Rest API endpoint of your WP site - importPostCount - Number of posts to be processed when running your migrat scriptb. Contentful:
- AccessToken - Get the API key from your Contentful https://www.contentful.com/developers/docs/concepts/apis/ - spaceId - YOu can get it from your App's URL https://www.contentful.com/help/spaces/find-space-id/ - environment - by default master - contentType - `blogPost` is default but it varies in the your target Contentful site https://www.contentful.com/help/content-types/ - contentFormat - richtext is the default - importPostCount - batch count to process -
Customize field mapping (if needed)
let fieldData = {
id: postData.id,
type: postData.type,
postTitle: postData.title.rendered,
slug: postData.slug,
content: postData.content.rendered,
publishDate: postData.date_gmt + '+00:00',
featuredImage: postData.featured_media,
tags: getPostLabels(postData.tags, 'tags'),
categories: getPostLabels(postData.categories, 'categories'),
contentImages: getPostBodyImages(postData)
}- Run the migration
npm run migrate
- Never commit sensitive credentials to version control
- The
config.jsfile is automatically excluded from git via.gitignore - Your Contentful access tokens provide full access to your space - keep them secure
- Consider using environment variables in production environments
This project uses config.js to store sensitive configuration data. The following files are included:
config.template.js- Template with example values (safe to commit)config.js- Your actual configuration (excluded from git).env.example- Alternative environment variable setup (if you prefer .env files)
IMPORTANT: There is no sandbox or test environment with this script. If you run this script, it will immediately attempt to publish your new posts and assets - I am not responsible for anything that goes wrong.