PHPackages                             pollora/colt - 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. [API Development](/categories/api)
4. /
5. pollora/colt

ActiveLibrary[API Development](/categories/api)

pollora/colt
============

Use WordPress backend with Laravel or any PHP framework

10.0.0(2mo ago)04.0k↓54.8%1MITPHPPHP ^8.2

Since Apr 8Pushed 2mo agoCompare

[ Source](https://github.com/Pollora/colt)[ Packagist](https://packagist.org/packages/pollora/colt)[ Docs](http://github.com/Pollora/colt)[ RSS](/packages/pollora-colt/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependencies (20)Versions (7)Used By (1)

Colt
====

[](#colt)

> **Note:** Colt is a fork of [Corcel](https://github.com/corcel/corcel), originally developed by [Junior Grossi](https://github.com/jgrossi).
> This fork is maintained by [Pollora](https://github.com/Pollora) and adapted for improved compatibility with Laravel and WordPress.

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

[![Actions Status](https://github.com/Pollora/colt/workflows/CI/badge.svg)](https://github.com/Pollora/colt/actions)[![Packagist](https://camo.githubusercontent.com/5173b6bb05ab2f553650a66e48121b70d7386e0041d2436576139055c27f0e46/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f706f6c6c6f72612f636f6c742e737667)](https://packagist.org/packages/pollora/colt)[![Packagist](https://camo.githubusercontent.com/093f301745a7d7f645bf72eba77bbd7aa2fdd1e19e4d55197e77111b010dece8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f706f6c6c6f72612f636f6c742e737667)](https://github.com/Pollora/colt/releases)

Colt 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 Colt with Laravel, but you're free to use it with any PHP project that uses Composer.

[Follow Pollora on Twitter](https://twitter.com/pollorawp)

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

[](#table-of-contents)

 Installing Colt
=================================================

[](#-installing-colt)

- [Version Compatibility](#version-compatibility)
- [Installing Colt](#installing-colt)
- [Database Setup](#database-setup)
- [Usage](#usage)
    - [Posts](#posts)
    - [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)

LaravelColt11.0.x`^8.0.0`12.0.x`^9.0.0` Installing Colt
=========================================================

[](#-installing-colt-1)

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

```
composer require pollora/colt

```

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

[](#configuring-laravel)

###  Laravel 5.5 and newer

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

Colt 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 `ColtServiceProvider` in your `config/app.php`:

```
'providers' => [
    /*
     * Package Service Providers...
     */
    Pollora\Colt\Laravel\ColtServiceProvider::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="Colt\Laravel\ColtServiceProvider"

```

Now you have a `config/colt.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 Colt in `config/colt.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 Colt)
        '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 Colt, so just set it into the Colt config file `config/colt.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 Colt 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
];
Pollora\Colt\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 `Colt\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 `\Pollora\Colt\Post`. Then set the connection name (if you want to override the Colt's default one) you're using, in this case `foo-bar`:

> Extending `Pollora\Colt\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.

```
