Editors note: This post was originally published in July 2015, but a lot has changed since then. Laravel has been upgraded to version 5.4 (it was 5.1 at the time of writing), and Wordpress is now up to version 4.7, and has integrated the WP-API plugin into its core. I’ve updated to article to reflect those changes.
Recently I was tasked with building an API with a Wordpress backend, to which some would say: just use the default REST API. Now, that’s perfectly reasonable, but what if you want to use custom post types, and not use the JSON structure Wordpress has decided upon? And what if you want better performance? My answer: Laravel + Wordpress.
If you’ve already tried to make one of these solutions, you’ll probably know that it’s not as easy as it sounds. Heck, that might even be why you’re here!
My solution is based on an environment where you have no say as to where your virtual host is directed (Laravel’s public-folder being the usual suspect), and using Laravel 5.4.
If you feel comfortable looking through Laravel and Wordpress code and figuring out how it all works, I’ve created a Github repository with a demo project that follows these guidelines.
I place Wordpress in the base directory of my site, and Laravel in a subfolder called “api”, leaving me with a structure like this:
Now that the files have been unpacked and structured, we’re off to code country!
There are basically three things you need to change to make Laravel work:
- The database config
- Add a base .htaccess file
- The public index.php file
To change the database config is pretty much a given. You need to change
api/config/database.php to reflect the
Wordpress influence. The biggest difference from the normal behavior, is that the config file should use the variables
defined in the
wp-config.php file (including the
$table_prefix global variable), like this:
Now the Laravel installation is listening directly to Wordpress’ configuration file.
The second thing you need to do, is add a base .htaccess file to the Laravel installation. You need to do this because Laravel is in a subdirectory, and needs all requests sent to the public folder. In your “api”-folder, add a .htaccess file with the following content:
The last thing you need, is to include Wordpress in Laravel’s public index.php file (
api/public/index.php). To do this, just add the following lines at the top of the file:
That’s basically what’s needed for Laravel to work in a Wordpress subdirectory.
Besides setting up wordpress to work with your database, you need to add some code. I know, it sucks. But it’s only one thing, well, one theme.
In you theme directory (
wp-content/themes) add a folder. I’ve called mine “laravel”. Now you just to add three small files:
These files are needed for Wordpress to understand that this is a theme.
This is just basic setup for a theme, nothing fancy here.
This code just redirects the user to the backend, since we don’t need a frontend for our API.
This file is more serious. I spent hours figuring this one out.
Since Wordpress is our default access to the site, it will add a 404 header when we access the API. Not only that, but Laravel will crash from the added headers.
Now you can access both the Wordpress and the Laravel installation.
Of course you need to set up Laravel to actually be able to read the Wordpress data structure. I’ve been working on a trait that acts as a transformation layer between the Wordpress database structure and a normal database structure:
To use this trait you simply include it in a model (like Post or Page), and add a few properties to the model: