PHPackages                             fabianmuema/corcel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. fabianmuema/corcel

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

fabianmuema/corcel
==================

Use WordPress backend with Laravel or any PHP framework upgrade for laravel 9 and 10

1.0.2(3y ago)021MITPHPPHP ^8.0.2

Since Apr 2Pushed 3y agoCompare

[ Source](https://github.com/fabianmuema/corcel)[ Packagist](https://packagist.org/packages/fabianmuema/corcel)[ Docs](http://github.com/fabianmuema/corcel)[ RSS](/packages/fabianmuema-corcel/feed)WikiDiscussions 6.0 Synced 1mo ago

READMEChangelogDependencies (11)Versions (4)Used By (0)

[![Corcel PHP](https://camo.githubusercontent.com/e9aceda0f5f9782e4c06847f9bbee164c288fa45dee5b934576be073019ae6cc/68747470733a2f2f692e696d6775722e636f6d2f66484d717754462e706e67)](https://camo.githubusercontent.com/e9aceda0f5f9782e4c06847f9bbee164c288fa45dee5b934576be073019ae6cc/68747470733a2f2f692e696d6775722e636f6d2f66484d717754462e706e67)
=======================================================================================================================================================================================================================================================================================================================================================

[](#)

**A collection of Model classes that allows you to get data directly from a WordPress database.**

[![Actions Status](https://github.com/corcel/corcel/workflows/CI/badge.svg)](https://github.com/corcel/corcel/actions)[![Packagist](https://camo.githubusercontent.com/8e6a372398d6673e0791a9e38d158e4db474795f4a4f3fd39c221bebd8a7afc0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a67726f7373692f636f7263656c2e737667)](https://packagist.org/packages/jgrossi/corcel)[![Packagist](https://camo.githubusercontent.com/de473c22c47fdb308255bcd9db8caccfc6a4f54610443d7f6e148b8260d430f6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a67726f7373692f636f7263656c2e737667)](https://github.com/jgrossi/corcel/releases)[![Test Coverage](https://camo.githubusercontent.com/f0f80612bb8d1107075ed374470c48367d3d6cadd9c10ee08ff240178ae3983d/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f636f7263656c2f636f7263656c2f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/corcel/corcel/coverage)[![Maintainability](https://camo.githubusercontent.com/de54c6a2adda5d96a262726a3b501ccc6722cc5a8d5d3831b245feb652202021/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f33646338313335656565373061653764613332352f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/corcel/corcel/maintainability)

Corcel is a collection of PHP classes built on top of [Eloquent ORM](https://laravel.com/docs/master/eloquent) (from [Laravel](http://laravel.com) framework), that provides a fluent interface to connect and get data directly from a [WordPress](http://wordpress.org) 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.

[Buy me a Coffee](https://ko-fi.com/A36513JF) | [Follow Corcel on Twitter](https://twitter.com/corcelphp)

Table of Contents
=================

[](#table-of-contents)

 Installing Corcel
===================================================

[](#-installing-corcel)

- [Version Compatibility](#version-compatibility)
- [Installing Corcel](#installing-corcel)
- [Database Setup](#database-setup)
- [Usage](#usage)
    - [Posts](#posts)
    - [Advanced Custom Fields (ACF) Integration](#acf)
    - [Custom Post Type](#custom-post)
    - [Single Table Inheritance](#single-tab)
    - [Taxonomies](#taxonomies)
    - [Post Format](#post-format)
    - [Pages](#pages)
    - [Categories &amp; Taxonomies](#cats)
    - [Attachments &amp; Revision](#attachments)
    - [Thumbnails](#thumbnails)
    - [Options](#options)
    - [Menu](#menu)
    - [Users](#users)
    - [Authentication](#auth)
    - [Running Tests](#tests)
- [Contributing](#contrib)
- [License](#license)

Version Compatibility
=====================

[](#version-compatibility)

LaravelCorcel5.1.x`~2.1.0`5.2.x`~2.2.0`5.3.x`~2.3.0`5.4.x`~2.4.0`5.5.x`~2.5.0`5.6.x`~2.6.0`5.7.x`~2.7.0`5.8.x`~2.8.0`6.0.x`^3.0.0`7.0.x`^4.0.0`8.0.x`^5.0.0`9.0.x`^6.0.0` Installing Corcel
=============================================================

[](#-installing-corcel-1)

You need to use Composer to install Corcel into your project:

```
composer require jgrossi/corcel

```

Configuring (Laravel)
---------------------

[](#configuring-laravel)

###  Laravel 5.5 and newer

[](#-laravel-55-and-newer)

Corcel wil register itself using Laravel's [Auto Discovery](https://laravel.com/docs/5.5/packages#package-discovery).

###  Laravel 5.4 and older

[](#-laravel-54-and-older)

You'll have to include `CorcelServiceProvider` in your `config/app.php`:

```
'providers' => [
    /*
     * Package Service Providers...
     */
    Corcel\Laravel\CorcelServiceProvider::class,
]
```

###  Publishing the configuration file

[](#-publishing-the-configuration-file)

Now configure our config file to make sure your database is set correctly and to allow you to register custom post types and shortcodes in a very easy way:

Run the following Artisan command in your terminal:

```
php artisan vendor:publish --provider="Corcel\Laravel\CorcelServiceProvider"

```

Now you have a `config/corcel.php` config file, where you can set the database connection with WordPress tables and much more.

 Database Setup
=======================================================

[](#-database-setup)

Laravel Setup
-------------

[](#laravel-setup)

Just set the database `connection` you want to be used by Corcel in `config/corcel.php`.

Let' suppose you have those following database connections in your `config/database.php` file:

```
// File: /config/database.php

'connections' => [

    'mysql' => [ // for Laravel database
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'mydatabase',
        'username'  => 'admin'
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
        'engine'    => null,
    ],

    'wordpress' => [ // for WordPress database (used by Corcel)
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'mydatabase',
        'username'  => 'admin',
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'wp_',
        'strict'    => false,
        'engine'    => null,
    ],
],
```

In this case you should want to use the `wordpress` connection for Corcel, so just set it into the Corcel config file `config/corcel.php`:

```
'connection' => 'wordpress',
```

Other PHP Framework (not Laravel) Setup
---------------------------------------

[](#other-php-framework-not-laravel-setup)

Here you have to configure the database to fit the Corcel requirements. First, you should include the Composer `autoload` file if not already loaded:

```
require __DIR__ . '/vendor/autoload.php';
```

Now you must set your WordPress database params:

```
$params = [
    'database'  => 'database_name',
    'username'  => 'username',
    'password'  => 'pa$$word',
    'prefix'    => 'wp_' // default prefix is 'wp_', you can change to your own prefix
];
Corcel\Database::connect($params);
```

You can specify all Eloquent params, but some are default (but you can override them).

```
'driver'    => 'mysql',
'host'      => 'localhost',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => 'wp_', // Specify the prefix for WordPress tables, default prefix is 'wp_'
```

 Usage
=====================================

[](#--usage)

 Posts
-------------------------------------

[](#-posts)

> Every time you see `Post::method()`, if you're using your own Post class (where you set the connection name), like `App\Post` you should use `App\Post::method()` and not `Post::method()`. All the examples are assuming you already know this difference.

> In the examples, every time you see `Post::method()` assume `Corcel\Model\Post::method()`.

```
// All published posts
$posts = Post::published()->get();
$posts = Post::status('publish')->get();

// A specific post
$post = Post::find(31);
echo $post->post_title;
```

Creating your own model classes
-------------------------------

[](#creating-your-own-model-classes)

Optionally you can create your own `Post` model (or Page, or whatever) which extends `Corcel\Post`. Then set the connection name (if you want to override the Corcel's default one) you're using, in this case `foo-bar`:

> Extending `Corcel\Model\Post` class can add flexibility to your project, once you can add custom methods and logic, according what you need to use from your WordPress database.

```
