WordPress migration to Statamic v3 (part 1)

Arlind Musliu cofounder at Lucky Media
Arlind Musliu

June 22, 2022 · 4 min read

WordPress migration to Statamic

Migrating data from WordPress to Statamic with Corcel

Are you considering a move from WordPress to Statamic CMS? If you’re dealing with a WordPress site built with Visual Composer and looking for a smooth transition, this guide will help you use the Laravel package Corcel to import your posts into Statamic v3 efficiently.

Before we started moving our data, we did some research and found a great article that explained how to install Corcel and move data simply. But we had a more complex situation, and that’s what we’ll talk about here.

Why Migrate to Statamic?

Statamic offers several benefits over traditional CMS platforms like WordPress:

  1. Flat-File Structure: Unlike WordPress, Statamic doesn’t require a database for content storage, which can lead to faster performance and easier version control.

  2. Flexibility and Control: Statamic provides more granular control over your website’s structure and content.

  3. Enhanced Security: With no database, the surface for potential attacks is reduced, making Statamic a more secure option.

  4. Simplified Management: Statamic’s CP is simple to use, even for people who aren’t tech-savvy.

Use Case: Removing Visual Composer tags

Many WordPress users look for other options for different reasons, like wanting better performance, a simpler interface, or more flexibility. Our client was tired of the difficult Visual Composer. While Visual Composer is a strong page builder, it puts its tags (in square brackets []) into the HTML content, which makes moving data harder. We need to take those out before we save them to Statamic. We also need to update internal links because Visual Composer uses IDs for them. The client also wanted to rename/remove some category names.

Installing Corcel

Corcel bridges the gap between WordPress and Laravel, allowing developers to query WordPress database content with Laravel’s Eloquent ORM. This makes it an excellent tool for importing WordPress data into other systems.

To install Corcel, you’ll need to run the following command with Composer:

composer require jgrossi/corcel

After installation, publish the configuration file with:

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:

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 simplify the migration, you can make a custom Laravel command:

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:

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();
    }
}

Importing WordPress Posts into Statamic

The next step is to change WordPress posts into Statamic entries. You’ll need to use the Statamic\Facades\Entry facade to accomplish this.

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::make()
                ->collection('posts')
                ->slug($post->post_name)
                ->date($post->post_date)
                ->data([
                    'title' => $post->title,
                    'categories' => $post->main_category,
                    'content' => $content,
                ])->save();
        }
	}
}

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 your post content have a lot of weird tags? Your client might use a plugin to add posts in WordPress. You will need to do more to get rid of those useless tags. In our example, the client used Visual Composer as a visual builder for their posts.

We had the following issues to deal with:

  • Remove 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 focus on the second part of the WordPress migration to Statamic v3.


Build Your Statamic Project with the #1 Statamic Agency

If you would rather have a professional team handle your WordPress to Statamic migration service, Lucky Media Statamic agency has migrated dozens of sites and handles the full process including content mapping, redirects, and editor training.

Technologies

Statamic
Arlind Musliu cofounder at Lucky Media
Arlind Musliu

Cofounder and CFO of Lucky Media

Stay up-to-date

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

Let’s chat

We partner with a limited number of brands each quarter to ensure senior-level attention on every project.

lokman and arlind headshots
Teamwork

Related posts

March 9, 2022

Event Listeners for Statamic
Statamic