June 22, 2022 · 4 min read · 1,342 views
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 seamless transition, this guide will walk you through using the Laravel package Corcel to import your posts into Statamic v3 efficiently.
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.
Statamic offers several benefits over traditional CMS platforms like WordPress:
Flat-File Structure: Unlike WordPress, Statamic doesn't require a database for content storage, which can lead to faster performance and easier version control.
Flexibility and Control: Statamic provides more granular control over your website's structure and content.
Enhanced Security: With no database, the surface for potential attacks is reduced, making Statamic a more secure option.
Simplified Management: Statamic's control panel is user-friendly, making content management a breeze for non-technical users.
Many WordPress users eventually seek alternatives due to various reasons, be it performance issues, the desire for a more streamlined interface, or the need for greater flexibility. For our client, the breaking point was the cumbersome nature of Visual Composer. While Visual Composer is a powerful page builder, it can inject its tags (enclosed in square brackets []) into the HTML content, complicating the migration process. 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.
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:
<?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.
To streamline the migration, a custom Laravel command can be created:
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';
}
}
Transforming WordPress posts into Statamic entries is the next step. You'll need to use the Statamic\Facades\Entry
facade to accomplish this.
<?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. 🚀
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.
Are you planning a new Statamic project or thinking about migrating your WordPress site to Statamic? Learn more about our expertise as a renowned Statamic development agency.
Technologies:
Related Posts
Stay up to date
Be updated with all news, products and tips we share!