Using Laravel 4 + Eloquent With WordPress

Last updated: June 28, 2017

Before I used Laravel, I used Codeigniter and I had the perfect workflow of being able to use WordPress as the administration panel, taxonomy system, authentication, users and post types.

I hate having to create administration panels from my previous experience working for a WordPress development agency, as the admin packages out there are complicated, don’t work correctly and are severely lacking in the features department.

In Laravel 4, the process for integrating WordPress is basically the same. You disable the theming functionality and include the WordPress blog-header bootstrapping file to include WordPress’ core functionality and features.

In the file public/index.php simply add the following:

define('WP_USE_THEMES', false);
require realpath(__DIR__.'/wordpress/wp-blog-header.php');

You now have WordPress integrated into your Laravel 4 installation. You can use functions inside of WordPress like wp_query, get_posts and more.

Using Laravel Eloquent With WordPress

Do you want to work directly with WordPress database tables using Eloquent? See below for an example to work with the wp_users table

<?php

class User extends Eloquent {

    // WordPress uses differently named fields for create and update fields than Laravel does
    const CREATED_AT = 'post_date';
    const UPDATED_AT = 'post_modified';
    
    // Set the table including database prefix used in WordPress
    protected $table = 'wp_users';

    // WordPress uses uppercase "ID" for the primary key
    protected $primaryKey = 'ID';

    // Hide the user_pass field
    protected $hidden = array('user_pass');

    // Whenever the user_pass field is modified, WordPress' internal hashing function will run
    public function setUserPassAttribute($pass)
    {
        $this->attributes['user_pass'] = wp_hash_password($pass);
    }

}

Some things worth noting in the above example when using Laravel with a WordPress table is that WordPress uses ALLCASE “ID” as the primary key, the created and updated fields are different to what Laravel expects by default too.

Based on the above example, you should be able to work with other tables with ease. If you require examples, I can happily provide some more.

 

Dwayne

 

9 thoughts on “Using Laravel 4 + Eloquent With WordPress

  1. Hi Dwayne,
    Thanks for the post introduction on using laravel and wordpress together.
    Would you provide me a sample on this? I am new to laravel. Any help would be appreciated.
    My email is altomare@outlook.com. Thanks again.

  2. Hi A.M,

    No worries, I am glad you found it useful. To be quite honest, instead of writing up some code, you will probably find value in the library Corcel. It is pretty up-to-date and I tested it not so long ago and it worked well: https://github.com/jgrossi/corcel – I believe it follows basically everything that I have outlined in this post. It allows you to interface with the WordPress database, so essentially WordPress becomes the CMS and Laravel becomes your front-end.

    If you get stuck with the library, I can help you out for sure.

  3. hi Dwayne ,thanx for the great post and the corcel link ,am kinda lost between why would i use wordpress when i have laravel ,

    this is because i know what wordpress can do but at the same time i find that i can do pretty much everything on laravel minus the bezzilion plugin that wordpress offer ,

    also from what i read ppl mostly go with WP for the admin panel ,well i can make custom panel to what the client needs and make it only avail for his admin username and pass ,so why would i use wordpress ?

  4. @ctf0 you would use wordpress if your client is very familar with WordPress and requests that the administration is done with WordPress.

    Of course you can use laravel for admin – and if that works for you, fine. The author was just providing an explanation on how to use with WP if you had the need to.

  5. Hi Dwayne,

    This is an eye opener hope may save tones of development time. All this time I was using redux not bad but limited
    Thanks I’ll try this today. I use wordpress with IIS hope laravel will not be an issue with IIS

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *