Laravel And WordPress Integration

Developing web applications can take a lot of time and resources. If you work with WordPress, you might be wondering if there’s a better way to integrate your development process with your Content Management System (CMS). 

Fortunately, one of the most popular Hypertext Preprocessing (PHP) programming frameworks, Laravel, can be integrated with WordPress. This combination can enable you to manage your web application development through the WordPress back end, offering a more streamlined workflow. 

In this article, we’ll learn more about Laravel and how it can be used alongside WordPress. We’ll also review the pros and cons of this approach. Let’s get started! 

What Is Laravel?

Laravel is a popular PHP framework. A framework can help speed up the web application development process. It provides a basic structure, and can help promote rapid application development (RAD) and avoid repetitive coding processes.

Additionally, using a framework like Laravel can also help developers of all levels build more stable applications, by ensuring the use of proper syntax and database interactions. If you’re new to PHP frameworks, you might want to familiarize yourself with a few important terms, including: 

  • Model View Controller (MVC). This is an architectural structure that separates the three components of PHP development: the Model, the View, and the Controller. This means a developer can isolate each part and make changes without impacting the other components. 
  • Object-Oriented Programming. In an object-oriented programming approach, such as PHP, structures are built with data or ‘Objects’, rather than function and logic. They can then be used throughout the program. Each Object has Properties and Behaviors that define its state and what it can do.
  • REST API. This is an architectural style used by most open-source web applications to form a workable exchange of information.
  • Composer. This is a tool used to manage the dependencies in your project. You can tell Composer what libraries your project requires, and Composer will monitor, install, and update them to ensure that your program will continue to function.  

It’s important to understand that while both Laravel and WordPress are PHP-based systems, there is still a bit of complicated coordination that is necessary for them to integrate in a functional way. 

Pros and Cons of Using Laravel and WordPress Together

Integrating Laravel with WordPress can be very effective, although it does require some higher-level programming knowledge. In addition, there are some distinct pros and cons to this approach that you’ll want to keep in mind.

The advantages of using Laravel alongside WordPress include:

  • Streamlining your web design and web application projects by sharing data. 
  • Creating web applications that look more like your website, to provide consistency in the customer experience. 
  • Retaining the simplicity of a familiar WordPress administration panel for clients, while maximizing Laravel for high-end development on the front end. 

There are also some drawbacks, however, such as:

  • WordPress does not natively support Composer, so integration can be tricky.
  • Integration ideally requires a full-time, skilled developer to be on staff and available to manage all aspects of the setup. 

You can check out an example of how this approach can be used by viewing the Laravel News website. There, Laravel was used to create the front-end experience, while bringing in data that was created in the user-friendly WordPress back end.

While there are quite a few benefits to integrating Laravel with WordPress, it should not be undertaken lightly. Keeping a developer on payroll would be best if this is an approach you want to explore.

Using Laravel With WordPress

Integrating Laravel with WordPress can be accomplished in a couple of ways. We’ll take a look at two: a more hands-on and manual approach, followed by a specialized plugin. The following steps assume that you’re already using Laravel.

Using WordPress Corcel

Corcel is a set of PHP Model classes that enable programmers to retrieve and use information directly from WordPress. It can function as a bridge between WordPress and Laravel.

By installing Corcel, you’ll be able to use WordPress as the back-end administration of your project for inserting data, such as posts or custom post types, into your Laravel application. 

Step 1: Install Corcel

To get started with Corcel, you’ll need to use Composer to install it. You can find and download Corcel on GitHub. In your terminal application, you’ll then need to execute the following command: 

composer require jgrossi/corcel

After that, you’ll want to jump into your Laravel installation and configure a few settings. 

Step 2: Configure Laravel for Corcel

If you’re running Laravel 5.5 or later, Corcel will register itself with Laravel’s AutoDiscovery function.

For older versions of Laravel, you’ll need to manually register Corcel and publish the configuration file using the following Artisan command in your terminal application: 

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

You’ll now have a config/corcel.php file that you can use to establish a database connection with WordPress. 

Step 3: Set Up Database Connection

Once your config/corcel.php has been created, you’ll open it to set up connections to your WordPress database. This is what enables Laravel to retrieve post information and other items from your WordPress database tables. 

Whatever database name you provide in your config/database.php file is what you’ll need to insert in your config/corcel.php file. This will enable Laravel and Corcel to use your WordPress database. 

Step 4: Set Your Project Parameters

There are a wide variety of ways you can use your new integration. You’ll need to refer to the documentation to determine how to set the parameters. This will enable you to establish connections for posts, custom post types, shortcodes, taxonomies, custom fields, and more. 

Using a Plugin

Currently, we don’t recommend using any plugins found in the WordPress Plugin Directory for Laravel integration. Most have not been maintained, and have not been tested with the latest versions of WordPress. There is, however, another option for integrating the two systems using a plugin-like solution.

Step 1: Install WordPress Pete

WordPress Pete is an application built to function like a plugin, which easily integrates Laravel with your WordPress back end. To get started, you can try WordPress Pete on up to two websites for free: 

WordPress Pete Pricing Table

Once you download the software, you’ll need Terminal to install it for either MacOS or Linux. You can use the following command on a Mac to get started: 

curl -o mac_installer.sh -L https://wordpresspete.com/mac_installer.sh && chmod 755 mac_installer.sh && sh mac_installer.sh

It may take some time for the entire package to install, but when it’s complete, you’ll be prompted to enter your computer’s admin password: 

Enter computer's admin password for WordPress Pete

You can then set the PHP version you want to use, and select Start Pete.

Step 2: Create a New WordPress Website

Once you’ve installed the WordPress Pete application, you’ll need to visit the test site to create a new WordPress website for your application. 

To do that, you’ll click on Create New WordPress in the upper right-hand corner. Next, you’ll name your installation and designate the project’s URL. From there, you can complete the WordPress installation process as normal by visiting your admin URL. 

Step 3: Configure WordPress Pete Settings

Once you’ve logged in to your new WordPress installation, you can navigate to the Plugins list. There, you’ll see a Lavarel Integration by WordPress Pete plugin. This is installed automatically when you create a new WordPress site with WordPress Pete.

You can now head over to Settings > Laravel Integration to complete the integration steps. This involves approving the integration, and allowing cookies to function on all of your subdomains.

Once you’ve enabled the option, you’ll need to select Update Options and log out of your WordPress installation. You can then log back into your WordPress dashboard, so the plugin can recognize the cookies setting.

Step 4: Create a New Integration

Now when you visit your WordPress Pete installation, you’ll have the option to create a new WordPress + Laravel integration. You can select the menu option and proceed to configure the settings for your new integration.

For example, if you name your new integration “dashboard”, you’ll be able to access the code by going to dashboard.yoururl.com. Back in your preferred code editor, you’ll now be able to connect to your installation and use the combined powers of Laravel and Corcel with WordPress!

Build Your Site on Your Own Terms With WP Engine

Web application development should be fast and fun. Responding to user demands and needs with new applications can be rewarding, and increase progress toward sales or revenue goals.

WP Engine is dedicated to sharing excellent developer resources, so you can focus on creating the best digital experience for your clients. Check out some of our many web hosting solutions for your next project! 

Get started.

Build faster, protect your brand, and grow your business with a WordPress platform built to power remarkable online experiences.