Skip to content

Tareq Hasan

Posted on

Eloquent ORM in WordPress

If you are a plugin writer like me and you write random WordPress plugins for various purposes, chances are you have to write many SQL queries for many cases. But if you look into modern PHP frameworks like Laravel, Symfony, Zend and every other framework, you will see they don’t write much raw SQL queries by hand. Instead, everyone is using ORM. In a few cases you might have to write raw SQL queries, with ORM you can do that as well.

If you look at the WordPress world, we are still stuck with the WPDB global object. It’s an Active Record class, but can’t be called an ORM. The main reason I like ORM is it reduces so much codes and facilitates many magical features without writing raw queries, instead of manipulating them using an Object Oriented like language. Also you can use many adapters for many databases (though it’s not possible right now in WordPress).

If you are writing a large plugin which involves many custom database tables, chances are you will get lost among those tables and queries very shortly. It happened to me as well and I was looking for using ORMs in WordPress. But there aren’t so many good tools for that. So I looked into Laravel as it’s hugely popular nowadays. Laravel uses a package named Eloquent as its ORM provider. As it’s a composer package, you can easily use Eloquent in any PHP application.

There are ways to use Eloquent independently, but it requires you to create a separate connection to database. But as WordPress is already connected to the database, why create a new connection? And if we use Eloquent like that, we will miss database reporting from various debugging plugins like debug-bar, query-monitor, etc.

That’s why I wrote a composer package to use Eloquent ORM inside WordPress using the same database connection using WPDB. The benefits are:

  • Eloquent is mainly used here as the query builder
  • WPDB is used to run queries built by Eloquent
  • It doesn’t create any extra MySQL connection
  • You get to use the WordPress debug tools/plugins

The package is not perfect, but please do checkout the Github repository for detailed information and usage instruction. Pull requests are always welcome.

View on Github

16 thoughts on “Eloquent ORM in WordPress”

  1. Hi, how to load your package in a wordpress existing ? At the root path, themes ? And how ?
    Thank you !!

    1. You can use the package in your plugin or theme. Check the github link and it has the package installation block. You have to use composer to install the package.

  2. Yep, i have well the /vendor directory in my directory themes, but how to load or where to load files ?

  3. composer.json is in the root path of wordpress
    vendor is in the root
    and i had : require_once(__DIR__ . '/vendor/autoload.php');
    BUT BLANK PAGE ??

  4. Ah shoot, thought it would be okay with PHP 5.3 but the dependencies need 5.4 which alienates ~40% of users if I use it. Thanks anyways :(

  5. Hi! Wanted to ask you what's the implementation of the "many to many" relationship. I've been trying to use the $this->belongsToMany Eloquent's way but it's not working