Tips for migrating from WordPress to Sanity, including using WPGraphQL and block tools.
28 replies
Last updated: Mar 10, 2021
B
anyone have a successful migration from wordpress to sanity? willing to share your process?
Mar 1, 2021, 6:22 PM
i think there were some other guide on that posted here (though the history doesn’t go back far enough) but our site has a lot of ACF and Woo so we’ve been using WPGraphQL for migrations. It’s not perfect though. in particular, you can’t pull drafts or private posts from WPGraphQL
Mar 1, 2021, 8:03 PM
B
are you saying you didn’t migrate the content? you just also request it alongside sanity content?
Mar 1, 2021, 8:16 PM
B
ahhh
Mar 1, 2021, 8:17 PM
B
interesting
Mar 1, 2021, 8:17 PM
B
so then you did mutations in sanity to create/save imported pages?
Mar 1, 2021, 8:18 PM
B
yup. sanity’s block tools are also helpful to transform HTML to sanity blocks: https://github.com/sanity-io/sanity/tree/next/packages/%40sanity/block-tools#htmltoblockshtml-blockcontenttype-options-html-deserializer
Mar 1, 2021, 8:19 PM
but it’s definitely not a 1-to-1 transform and we’re still in the middle of auditing our pages
Mar 1, 2021, 8:19 PM
B
but at least this gets the content partially on there
Mar 1, 2021, 8:20 PM
if it’s only blog posts, then i think the wordpress REST API would be helpful too
Mar 1, 2021, 8:21 PM
do you have any code samples you could share of your mutations? I honestly haven’t worked with them yet
Mar 1, 2021, 8:24 PM
i don’t atm but it would go something like this:
import SanitySchema from '@sanity/schema'; import blockTools from '@sanity/block-tools'; import Sanity from '@sanity/client'; import { JSDOM } from 'jsdom'; const contentType = SanitySchema.compile({ name: 'default', types: [ { name: 'blocks', type: 'array', of: [{ type: 'block' }], }, ], }).get('blocks'); const htmlToBlocks = (html: string) => { return blockTools.htmlToBlocks(html, contentType, { parseHtml: (html) => new JSDOM(html).window.document, }); }; const sanity = Sanity({/* init sanity client */}); async function main() { const allPosts = await getAllWordPressPostsViaTheRestApi(); const t = sanity.transaction(); for (const post of allPosts) { t.createOrReplace({ _id: post.id.toString(), content: htmlToBlocks(post.content.rendered), }); } await t.commit(); } main().catch(e => { console.error(e); process.exit(1); });
Mar 1, 2021, 8:30 PM
B
i haven’t tested the above but i think it gives the idea
Mar 1, 2021, 8:35 PM
B
awesome. thanks
Mar 1, 2021, 8:35 PM
S
i’ll definitely check it out
Mar 1, 2021, 8:35 PM
S
Apologies if this is old hat but
https://github.com/kmelve/wordpress-to-sanity
user Y
’s demo, while it hasn't been updated in some time, may be helpful!https://github.com/kmelve/wordpress-to-sanity
Mar 10, 2021, 12:42 AM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.