Using Eloquent in WordPress Plugin Development
Tags: wordpress, eloquent, plugin
Published: 2019-06-17 03:02:49

---
title: Using Eloquent in WordPress Plugin Development
published: true
tags: WordPress,Eloquent,plugin
canonical_url: https://upnrunn.com/blog/2019/05/using-eloquent-in-wordpress-plugin-development/
---

WordPress is great but I really hate it when I have to write SQL queries. Most of the developers I have seen don’t really like writing SQL Queries. WordPress Comes with $wpdb class but you still have to write sql queries. $wpdb is fine when you have small number of queries to make but If you have ever encountered a situation where you have work with lots of custom database tables and have to join them with different tables, you know the pain.

I came across similar situation and instead writing ugly sql queries , I came up with a solution using Eloquent. Eloquent is ORM developed by Laravel framework. This is very easy to work with and you can still write sql queries with Eloquent if you want to. I am not going to talk about features of Eloquent in this article. You can look into official [eloquent documentation](https://laravel.com/docs/5.4/eloquent) for more information about Eloquent.

 

### Lets start setting up our plugin.

I assume you have a working WordPress installation and You have composer installed in your machine. If you are not sure what composer is, Composer is dependency manager for php. You can visit [composer’s official page](https://getcomposer.org/) for more information and download.

Lets navigate to WordPress  plugins directory and create a folder for our plugin, I am going to name my plugin _ **“wpdrift-client”** _.

I have composer installed globally in my machine, if your composer is not global, you might want to copy composer.phar file into your plugin directory and use “ **_composer.phar_** ” instead of  “ **_composer_** ” when making composer commands. lets require illuminate\database package from composer. Open your terminal window and cd into the plugin folder _(i.e. wordpress-installation/wp-content/plugins/wpdrift-client)_ and type following command:

```
composer require illuminate/database
```

Now your plugin folder should look something like this:

![](https://i0.wp.com/wpdrift.com/blog/wp-content/uploads/2017/07/Screen-Shot-2017-07-21-at-4.40.55-PM-300x52.png?resize=300%2C52&ssl=1)

Composer has generated _vendor_ directory which includes are package, _composer.json_ file and _composer.lock_ file. Lets open _composer.json_ and setup our namespace. Lets edit it to look something like following code:

```
{ "require": { "illuminate/database": "^5.4" }, "autoload": { "psr-4": { "Models\\": "app/models", "Controllers\\": "app/controllers" } } }
```

Run a composer command from your terminal

```
composer dump-autoload
```

We have setup namespace for our models and controllers now lets create these folders. We will be creating a folder named _ **“app”** _ and inside that we will create two folders _ **“models”** _ and _ **“controllers”** _ which will store our Models and Controllers respectively. You can name your namespace anything you like.

We have installed Eloquent successfully, lets go ahead and create other files for plugin. We will create a main plugin file _“wpdrift-clients.php”_, a blank _“index.php”_ file, _“assets”_ folder which will store our css, js, images and fonts, we will also create a folder named _views_ which will store views for our plugin. We will also create a folder called _“core”_ which will be responsible for storing other files like plugin activation hook, connecting eloquent to database and helpers.

After creating these files and folders, your plugin directory should look something like this:  
 ![](https://i2.wp.com/wpdrift.com/blog/wp-content/uploads/2017/07/Screen-Shot-2017-07-27-at-5.08.05-PM-600x377.png?resize=600%2C377&ssl=1)

Let me explain what I did here:

We are creating a database table named “clients” (with prefix), _core/create\_db\_tables.php_ includes code to create database, _core/class-enqueue.php_ is responsible for styles and scripts required for plugin. _helpers.php_ includes helper functions. Pretty much most of the things are included inside _core/init.php_

Here are the codes for these files:

**_Main plugin file : wpdrift-client.php_**

```
/\* Plugin Name: WpDrift Clients Plugin URI: http://wpdrift.com Description: Custom plugin using Eloquent Version: 1.0 Author: WPDrift Author URI: http://wpdrift.com License: GPL2 License URI: https://www.gnu.org/licenses/gpl-2.0.html Text Domain: wpdrift-clients Domain Path: /languages \*/ if(! defined('ABSPATH')) exit; if( ! defined('WPDRIFT\_CLIENTS\_URl')){ define('WPDRIFT\_CLIENTS\_URl',plugin\_dir\_url(\_\_FILE\_\_)); } if( ! defined('WPDRIFT\_CLIENTS\_PATH')){ define('WPDRIFT\_CLIENTS\_PATH',plugin\_dir\_path(\_\_FILE\_\_)); } require WPDRIFT\_CLIENTS\_PATH.'/core/create\_db\_tables.php'; register\_activation\_hook(\_\_FILE\_\_,'wpdrift\_clients\_custom\_tables'); if(! class\_exists('Wpdrift\_Clients')){ class Wpdrift\_Clients { function \_\_construct(){ add\_action('plugins\_loaded',[$this,'include\_dependencies']); } function include\_dependencies() { include WPDRIFT\_CLIENTS\_PATH.'/core/init.php'; } } } new Wpdrift\_Clients();
```

**_core/init.php_**

```
require WPDRIFT\_CLIENTS\_PATH.'/vendor/autoload.php'; /\*database connection\*/ $capsule = new Illuminate\Database\Capsule\Manager; $capsule-\>addConnection(['driver' =\> 'mysql', 'host' =\> DB\_HOST, 'database' =\> DB\_NAME, 'username' =\> DB\_USER, 'password' =\> DB\_PASSWORD, 'charset' =\> 'utf8', 'collation' =\> 'utf8\_unicode\_ci', 'prefix' =\> '',]); $capsule-\>setAsGlobal(); $capsule-\>bootEloquent(); // enqueue styles and scripts with class-enqueue.php // helper functions helper.php include\_once WPDRIFT\_CLIENTS\_PATH.'/core/helpers.php'; include\_once WPDRIFT\_CLIENTS\_PATH.'/core/class-enqueue.php'; // add ajax actions $actions = new Controllers\ActionController(); add\_action('wp\_ajax\_create\_client',[$actions,'create']); add\_action('wp\_ajax\_delete\_client',[$actions,'delete']); // shortcode add\_shortcode('wpdrift\_clients',function(){ wpdrift\_clients\_load\_view('clients',['title'=\>'Clients list']); });
```

**_core/class-enqueue.php_**

```
if(! class\_exists('Wpdrift\_Clients\_Enqueue')){ class Wpdrift\_Clients\_Enqueue { function \_\_construct(){ add\_action('wp\_enqueue\_scripts',array($this,'enqueue')); } function enqueue() { wp\_enqueue\_style('wpdrift-clients',WPDRIFT\_CLIENTS\_URl.'assets/css/wpdrift-clients.css'); wp\_enqueue\_script('jquery'); wp\_enqueue\_script('wpdrift-clients',WPDRIFT\_CLIENTS\_URl.'assets/js/wpdrift-clients.js',array('jquery'),'1.0',true); } } new Wpdrift\_Clients\_Enqueue(); }
```

**_core/helpers.php_**

```
function wpdrift\_clients\_load\_view($file,$data=[]){ if(! empty($data)){ extract($data); } if(file\_exists(WPDRIFT\_CLIENTS\_PATH.'views/'.$file.'.php')){ include\_once WPDRIFT\_CLIENTS\_PATH.'views/'.$file.'.php'; } else{ echo 'File not found in '.WPDRIFT\_CLIENTS\_PATH.'views/'.$file.'.php'; } } function get\_clients(){ $clients = Models\Client::all(); return $clients; }
```

Basically, we are just displaying a view with shortcode. Shortcode will trigger a function _wpdrift\_clients\_load\_view() (Location: core/helpers.php)_ , this function will extract array argument and include a view from _views_ folder.

View file includes a table which is displaying dynamic data with a function _get\_clients() (Location: core/helpers.php)_, this function gets data from _Client_ Model.

each dynamic data has a delete button which is a form with a ajax action, (_Action defined on core/init.php_), call back for these actions are stored in _app/controllers/ActionController.php_, this controller is using _app/models/Clinet.php_ to interact with clients table.

**_app/controllers/ActionController.php_**

```
namespace Controllers; use Models\Client as Client; class ActionController { function create(){ $name = $\_POST['name']; $email = $\_POST['email']; $phone = $\_POST['phone']; $purchased\_service = $\_POST['service']; $client = new Client; $client-\>name = $name; $client-\>email = $email; $client-\>phone = $phone; $client-\>purchased\_service = $purchased\_service; $client-\>save(); $response = ['response\_type' =\> 'success', 'response\_data' =\> [ 'name' =\> $clinet-\>name, 'email' =\> $clinet-\>email, 'phone' =\> $clinet-\>phone, 'service' =\> $clinet-\>purchased\_service,] ]; return $response; die(); } function delete(){ $client\_id = $\_POST['client\_id']; $client = Client::find($client\_id); if($client){ $client-\>delete(); return 'success'; die(); } return false; die(); } }
```

**_app/models/Client.php_**

```
namespace Models; use Illuminate\Database\Eloquent\Model as Model; class Client extends Model { protected $table; public $timestamps = false; function \_\_construct() { parent::\_\_construct(); $this-\>table = $this-\>get\_table\_name('clients'); } private function get\_table\_name($name) { global $wpdb; return $wpdb-\>prefix.$name; } }
```

As You can see, we don’t have much code on our model, all we are telling model is to use _prefix\_clients_ table, Eloquent by default expects table to have _created\_at_ and _updated\_at_ column. So, by defining _public $timestamps = false;_ we are disabling that feature. Eloquent assumes you have a primary key table with the name _id_, if you are thinking of using some other name, you will have to define one more property called _$primaryKey_ . For example, if you are creating a model for WordPress native _Users_ table, it would look something like this:

```
namespace Models; use Illuminate\Database\Eloquent\Model as Model; class User extends Model { protected $table; public $timestamps = false; protected $primaryKey = 'ID'; function \_\_construct() { parent::\_\_construct(); $this-\>table = 'wp\_users'; $this-\>table = $this-\>get\_table\_name('users'); } private function get\_table\_name($name) { global $wpdb; return $wpdb-\>prefix.$name; } function meta() { return $this-\>hasMany('App\Usermeta'); } function posts() { return $this-\>hasMany('App\Post'); } }
```

In the above example _meta and posts_ method define relationship with other Models “Usermeta and Post”, which can use usermeta and posts table.

We built a very basic plugin using eloquent orm. But this article is not about how big the plugin is but structuring your plugin to use _MVC_ architecture and using Eloquent orm with your plugin. This article only focused on using MVC and Eloquent, you will have to implement security when you build your real plugin.

Hence, if you are using creating a big custom plugin with lots of custom tables, I believe this approach will be very manageable and easy where as I would not suggest you to use this approach to publish your plugin in _wordpress.org_ as it makes extra database connection and user is using 5 plugins with similar approach, they will have 6 database connections.

**[Download code](https://wpdrift.com/blog/wp-content/uploads/2017/07/wpdrift-clients-2.zip)**

The post [Using Eloquent in WordPress Plugin Development](https://upnrunn.com/blog/2019/05/using-eloquent-in-wordpress-plugin-development/) appeared first on [upnrunn® | Build & Deploy Powerful Application](https://upnrunn.com).