• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Using Laravel 4 + Eloquent With WordPress

Laravel, Wordpress · September 16, 2014

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

Leave a Reply Cancel reply

11 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
A.M.
A.M.
8 years ago

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.

0
Dwayne
Dwayne
Author
8 years ago

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.

0
ctf0
ctf0
8 years ago

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 ?

0
derekjadams
derekjadams
8 years ago

@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.

0
wecode
wecode
7 years ago

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

0

Primary Sidebar

Popular

  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • Thoughts on the Flipper Zero
  • Waiting for an Element to Exist With JavaScript
  • How To Paginate An Array In Javascript
  • How To Mock uuid In Jest
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • How to Use Neural DSP Archetype Plugins With the Quad Cortex
  • How To Decompile And Compile Android APK's On A Mac Using Apktool
  • NBN Box Installed Inside of Garage, Where Do You Put The Modem?

Recent Comments

  • Kevmeister68 on Start-Ups and Companies That Embrace Work From Anywhere Will Be More Likely to Survive the Coming Recession in 2023
  • kevmeister68 on What Would Get People Back Into the Office?
  • Dwayne on PHP Will Not Die
  • Dwayne on How to Create a Blockchain With TypeScript
  • kevmeister68 on PHP Will Not Die

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz