Integrating WordPress into Laravel Using the Corcel Package

Integrating WordPress into Laravel Using the Corcel Package

Adam Morgan

When it comes to developing new web applications, the whole process needs to be fast-paced and fun.  It’s important to remember that each web application will come with its own set of requirements and lifecycle.  For those of you unfamiliar with WordPress, this platform is a powerful CMS written in PHP.  Basically, this means you can create and produce new applications extremely quickly.  You can make sure your WordPress reflects any recent PHP conventions or changes by using it with an additional framework.  In this article, we’re going to be taking a look at how you can integrate WordPress and Laravel using the Corcel package.

What is Corcel?

The WordPress administration panel does offer numerous plugins that enable you to include fields, images, post types, crops and a lot more, extremely quickly!  Also, the best thing about the WordPress administration panel is that all of this works seamlessly!  Developers created Corcel to help you get data from your WordPress database with ease.  The Corcel package will allow you to use WordPress as your admin panel and retrieve all your data using Eloquent, with any PHP framework but more specifically with Laravel.  So, what do you need to do this?  All you need is a WordPress installation, your Laravel PHP application and the composer Corcel package.

If you’re unfamiliar with Corcel, this composer package offers a model collection to help you retrieve Pages, Menus, Posts and more.  This collection of different classes was initially created to help users retrieve WordPress database data using a better syntax.  In fact, you’ll even be able to have different database connections.  Corcel uses the Eloquent ORM, which was specifically created to work with the Laravel framework.  These connections will work for both your WordPress application and the Laravel app.  It’s important to note that you can use Corcel for any type of PHP project, with any framework.  We are, however, going to be focussing on using Corcel with the Laravel framework in this article.

Getting Started with Corcel

To install Corcel, you’ll have to use Composer.  All you need to install Corcel for your current project is this simple snippet of code: composer require jgrossi-corcel.  After this, you’ll then need to configure Corcel using Laravel.  This is done by including the CorcelServiceProvider in your config/app.php.  You’ll then need to configure the config file to ensure your database is setup correctly.  This also means you will be able to register custom post types and shortcodes.  Also, this means you will have a config file where you’ll be in control of the database connection with your WordPress tables.

To ensure that your database is set up correctly, you’ll need to set the connection you want Corcel to use in the config/corcel.php file.  Another option you have when integrating Laravel and WordPress with Corcel is to create your own model classes.  Your own post model or page will extend the Corcel\Post.  Extending this offers added flexibility to your project.

Are you already using Corcel to integrate WordPress and Laravel?  Let us know about your own experience using the Corcel package in the comments below.