Laravel and WordPress combined

WordPress and Laravel are popular frameworks that are good at what they are designed for. WordPress excels in managing content with revisions, taxonomies and a pretty good UI. Laravel is good in managing structured data with its eloquent models, routing and securing access to routes. Sometimes a project requires both the management of a lot of content and also handling structured content. The question arises: what are you going to use?

The answer is: you can use both for what they are good for. In my case this is: using WordPress as the backend CMS and Laravel as the frontend display engine but also have access to Laravel from the WordPress backend, so I can manage structured data from within the WordPress backend by making use of Laravel. So I need access to Laraval from the WordPress backend and from Laravel I need access to WordPress.

See my example project: https://github.com/michielvaneerd/wp-laravel


Because I use WordPress as the backend and Laravel as the frontend, I install Laravel in the root of the directory and maken Apache use this as the document root. Laravel and WordPress are placed next to each other, so they stay 2 separate platforms and can easily be updated.

/laravel/public/wordpress (symlink to /wordpress)

My Apache conf sets the DocumentRoot to /var/www/html/laravel/public.

So now I can login into mysite.com/wordpress/wp-admin.

Use Laravel in WordPress backend

First create a WordPress plugin and add the code below:

function my_laravel_start() {
    require __DIR__ . '/../../../../laravel/vendor/autoload.php';
    $app = require_once __DIR__ . '/../../../../laravel/bootstrap/app.php';
    $kernel = $app->make(Kernel::class);

This code is almost the same as the code you see in /laravel/public/index.php (which is the entry point for Laravel) except that here er don't handle requests.

Now I can add for example a settings menu item that displays a page.

function wpdocs_register_my_custom_menu_page() {
        __( 'Custom Menu Title', 'textdomain' ),
        'custom menu',
add_action( 'admin_menu', 'wpdocs_register_my_custom_menu_page' );

En in de functie die de pagina toont kan ik Laravel Blade gebruiken bijv. of modellen:

function my_laravel_settings_page() {
    $view = view('backend.settings-index', [
        'stats' => Stat::all()
    echo $view->render();

WordPress gebruiken in Laravel frontend

Je kunt dit op 2 manieren doen:

  1. WordPress includen in Laravel zodat je alle WordPress functies kunt gebruiken.
  2. Vanuit Laravel de WordPress REST API aanspreken.

WordPress includen in Laravel

Het voordeel van WordPress includen is dat je alle bekende functies kunt aanspreken, inclusief zelfs de template functies zoals get_header(). Het grote nadeel is dat er clashes zijn in globale functies die beide frameworks gebruiken, zoals __(). In WordPress wordt niet gecontroleerd of deze functie al bestaat en je krijgt dan een error. De enige optie om dit op te lossen is om WordPress als allereerste in te laden, want Laravel checkt dit wel. Maar het nadeel hiervan is dat je dus altijd de WordPress __() functie gebruikt.

Include WordPress in /laravel/public/index.php

require __DIR__ . '/wordpress/wp-load.php';

Dit moet je als eerst doen, anders krijgt je fout op __().

WordPress aanspreken via API

In een Laravel controller kun je doen:

use Illuminate\Support\Facades\Http;
Tag: , | Category: