Interact with a WordPress Database from a Laravel Application

Laravel Last Updated:
Interact with a WordPress Database from a Laravel Application
Interact with a WordPress Database from a Laravel Application
  1. REST API or Database Connection?
  2. Setup Database Connection
  3. Add The Credentials To The Environment Variables File
  4. Install Corcel
  5. Basic Queries

REST API or Database Connection?

If your application is currently interacting with WordPress via a REST API connection then you may need to consider which routes can be converted into a simple database connection. As we would be interacting with WordPress using the Eloquent ORM you will not have access to WordPress functions and APIs such as WP_Query and get_post_meta

In this post we will use updating a Woocommerce products stock level and also performing queries on Orders as examples when looking at whether we need to use a database connection or a REST API route.

Setup Database Connection

To setup the database connection you will need the database credentials from your wp-config.php file on the WordPress installation. These are used by Laravel to connect to the database the same way WordPress does.

If your WordPress installation is on the same domain then you can still use localhost as the DB_HOST value. However, if your WordPress installation is on another host then you will need to setup remote SQL. Depending on your hosting provider the setup for this will be different. For cPanel you only need to browse to the Remote SQL menu on your cPanel and add the I.P address of the Laravel application, which will allow the Laravel application to connect to the WordPress database.

After you've setup remote SQL you need to use the WordPress' I.P address as the DB_HOST value in the below step.

Add The Credentials To The Environment Variables File

You should add the following to your .env file in your Laravel project making any changes to the values and filling in the blanks. We will use these values in our config file in the next step.

WORDPRESS_DB_HOST=localhost
WORDPRESS_DB_DRIVER=mysql
WORDPRESS_DB_PORT=3306
WORDPRESS_DB_DATABASE="wordpress_db"
WORDPRESS_DB_USERNAME="wordpress_db_user"
WORDPRESS_DB_PASSWORD=
WORDPRESS_DB_PREFIX="wp_"

Install Corcel

Corcel is a collection of PHP classes built on top of Eloquent ORM (from Laravel framework), that provides a fluent interface to connect and get data directly from a WordPress database.

You can use WordPress as the backend (administration panel) or CMS, for inserting posts, custom types, etc, and any other PHP app in the other side querying those data (as a Model layer). It's easier to use Corcel with Laravel, but you're free to use it with any PHP project that uses Composer.

composer require jgrossi/corcel
php artisan vendor:publish --provider="Corcel\Laravel\CorcelServiceProvider"

Setup the Corcel Config File

Inside the config/corcel.php we need to set the value of connection to wordpress, then we need to actually create the wordpress database connection in our config/database.php

<?php

return [
    'connection' => 'wordpress',
];

Create the WordPress connection inside config/database.php using the environment variables we added previously.

<?php

return [
    'connections' => [
        'wordpress' => [
            'driver'    => env('WORDPRESS_DB_DRIVER', 'mysql'),
            'host'      => env('WORDPRESS_DB_HOST', 'localhost'),
            'database'  => env('WORDPRESS_DB_DATABASE'),
            'username'  => env('WORDPRESS_DB_USERNAME'),
            'password'  => env('WORDPRESS_DB_PASSWORD'),
            'port'      => env('WORDPRESS_DB_PORT', 3306),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => env('WORDPRESS_DB_PREFIX', 'wp_'),
            'strict'    => false,
            'engine'    => null,
        ],
   ]
];

After this step we are now able to start using the database connection!

Basic Queries

We can query WordPress posts just like we would query anything in our Laravel Application.

use Corcel\Model\Post;
        
Route::get('/', function () {
    return Post::first();
});

Copyright © 2022 | bonnick.dev

Privacy Policy