WordPress migration to Statamic v3 (part 1)

WordPress migration to Statamic v3 (part 1)

Published: July 25, 2022

Updated: July 27, 2022

statamic

wordpress

migration

cms

laravel

corcel

WordPress to Statamic

In this post we will focus on importing WordPress posts to Statamic v3 by using the Laravel package Corcel. Before starting our migration we did some research and found out about this great article explaining the installation process of Corcel and simple data migration. However, our use case was more complicated and that's what we will explain here.

Use Case: Stripping Visual Composer tags

Our client has a WordPress website that is built with Visual Composer. After many struggles, they decide it's time to move on from WordPress and look for alternatives. We proposed to migrate their website to Statamic CMS and the client agreed. However, Visual Composer adds its own tags (inside of [] ) to the HTML content and we need to strip those out before saving them to Statamic. We also need to relink internal links because Visual Composer uses ids for them. The client also wanted to rename/remove some category names.

Installing Corcel

Corcel is a fantastic Laravel package that allows us to get data directly from a WordPress database. It can be useful for keeping WordPress as the backend while you choose your preferred technologies for the frontend. In our case, we can use it for importing the data we have on our WordPress site database.

Installation can be done through composer:

composer require jgrossi/corcel

Then, we can publish the configuration file to make additional changes:

php artisan vendor:publish --provider="Corcel\Laravel\CorcelServiceProvider"

We need to navigate to config/database.php and modify the mysql connection. We can rename it WordPress and modify it for connecting to your database. This is our configuration:

<?php

return [
  'wordpress' => [
    'driver' => 'mysql',
    'url' => env('DATABASE_URL'),
    'host' => env('DB_HOST', '127.0.0.1'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => 'wp_',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
  ],  
]

We also need to tell Corcel which database connection to use. We need to edit config/corcel.php:

'connection' => 'wordpress',

That's it for Corcel. Now we can continue with our migration process.

Custom command for migration

To make it easier, we will create a Laravel command that can be executed through the terminal. This can be done with:

php artisan make:command ImportWordPress

For a simple use case, we can use the following code to get the latest 100 posts from our WordPress database:

<?php

namespace App\Console\Commands;

use Corcel\Model\Post;
use Illuminate\Console\Command;

class ImportWordPress extends Command
{
    protected $signature = 'import:wp';

    protected $description = 'Import posts from WordPress';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $posts = Post::type('post')
              ->orderBy('post_date', 'desc')
              ->published()
              ->limit(100)
              ->get();

        return 'WordPress posts successfully retrieved';
    }
}

Adding the posts to Statamic

Now that we have our posts, we can include use Statamic\Facades\Entry; to transform our posts as Statamic entries.

<?php

namespace App\Console\Commands;

use Statamic\Facades\Entry;
use Corcel\Model\Post;
use Illuminate\Console\Command;

class ImportWordPress extends Command
{
    protected $signature = 'import:wp';

    protected $description = 'Import posts from WordPress';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $posts = Post::type('post')
              ->orderBy('post_date', 'desc')
              ->published()
              ->limit(100)
              ->get();

        foreach ($posts as $post){
          $entry = Entry::make()
                ->collection('posts')
                ->slug($post->post_name)
                ->date($post->post_date)
                ->data([
                    'title' => $post->title,
                    'categories' => $post->main_category,
                    'content' => $content,
                ]);

          $entry->save();
        }
        
        return 'WordPress posts successfully migrated to Statamic';
    }
}

Done! Your WordPress site doesn't use any crazy plugin that adds all sorts of tags to your content HTML file. You're so lucky. 🚀

Not enough?!

Does the post content hold a bunch of weird tags? Your client probably uses a plugin for inserting posts through WordPress. You will need to make more improvements to remove all those un-useful tags. In our case study, the client used Visual Composer as a visual builder for their posts.

We had the following issues to deal with:

  • Strip all Visual Composer tags from the HTML content
  • Relink internal page links to replace Visual Composer id links
  • Relink internal image URLs to replace Visual Composer id links
  • Importing the thumbnail image and using it as a featured image
  • Replace old category names with new categories

To keep this post simple, we will explain more details in the following post.

If you need to migrate your WordPress site to Statamic let's get in touch.

Stay up to date

Be updated with all news, products and tips we share!