Published:June 22, 2022
Updated: November 28, 2023
Views: 1,097
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.
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.
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.
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';
}
}
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. π
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.
Lucky Media is proud to be recognized as the #1 best Statamic Agency by Clutch, a leading B2B ratings and reviews platform.
At Lucky Media, we offer a range of services including website development, web application development, and mobile apps development. We specialize in Statamic, React Native, Next.js, AI and ML solutions. We also provide staff augmentation and TALL stack development services.
For more insights into our work, check out our case studies on revolutionising lead generation with AI, customized coaching site, healthcare digitization, next-level performance, lead generation and patient journey, WordPress to Statamic migration, and improving user experience. These case studies provide a glimpse into how we tailor our technology choices to meet specific client needs and deliver exceptional results.
Related Posts
Stay up to date
Be updated with all news, products and tips we share!