PHP Classes
elePHPant
Icontem

Corcel (Laravel + WordPress): Use the WordPress backend with any PHP application

Recommend this page to a friend!

  Author Author  
Name: Junior Grossi is available for providing paid consulting. Contact Junior Grossi .
Classes: 1 package by
Country: Brazil Brazil
Age: 32
All time rank: 3454275 in Brazil Brazil
Week rank: 67 Up2 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 1x

Winner: 1x


  Detailed description   Download Download .zip .tar.gz   Install with Composer Install with Composer  
This package allows using the WordPress backend with any PHP application.

It allows to use WordPress backend (admin panel) with any PHP project.

Originally created to work with Laravel, this package uses que Eloquent ORM to connect you current PHP application to the WordPress database, getting all data you need, without using WordPress classes and methods, allowing to use a entire MVC environment using WordPress as CMS behind the scenes.

Details

Corcel

> This package allows you to use WordPress as backend (admin panel) and retrieve its data using Eloquent, with any PHP project or even framework.

Travis Packagist Packagist

<a href='https://ko-fi.com/A36513JF' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://az743702.vo.msecnd.net/cdn/kofi4.png?v=0' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

Twitter Follow

Corcel is a class collection created to retrieve WordPress database data using a better syntax. It uses the Eloquent ORM developed for the Laravel Framework, but you can use Corcel in any type of PHP project, with any framework, including Laravel.

This way, you can use WordPress as the backend (admin panel), to insert posts, custom types, etc, and you can use whatever you want in the frontend, like Silex, Slim Framework, Laravel, Zend, or even pure PHP (why not?). So, just use Corcel to retrieve data from WordPress.

Installation

To install Corcel, just run the following command:

composer require jgrossi/corcel

Usage

I'm using Laravel

If you are using Laravel you do not need to configure database again. It's all already set by Laravel. So you have only to change the config/database.php config file and set yours connections. You can use just one connection or two (one for your Laravel app and another to Corcel). Your file will look like this:

<?php // File: /config/database.php

'connections' => [

    'mysql' => [ // this is your Laravel database connection
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'app',
        'username'  => 'admin'
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
        'engine'    => null,
    ],

    'wordpress' => [ // this is your Corcel database connection, where WordPress tables are
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'corcel',
        'username'  => 'admin',
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'wp_',
        'strict'    => false,
        'engine'    => null,
    ],

],

Now you should create your own Post model class. Laravel stores model classes in app directory, inside App namespace (or the name you gave it). Your Post class should extends Corcel\Post and set the connection name you're using, in this case wordpress:

<?php // File: app/Post.php

namespace App;

use Corcel\Post as Corcel;

class Post extends Corcel
{
    protected $connection = 'wordpress';
}

So, now you can fetch database data:

$posts = App\Post::all(); // using the 'wordpress' connection
$posts = Corcel\Post::all(); // using the 'default' Laravel connection

I'm using another PHP Framework

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 = array(
    '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_'

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.

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

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

// Filter by meta/custom field
$posts = Post::published()->hasMeta('field')->get();
$posts = Post::hasMeta('acf')->get();

You can retrieve meta data from posts too.

// Get a custom meta value (like 'link' or whatever) from a post (any type)
$post = Post::find(31);
echo $post->meta->link; // OR
echo $post->fields->link;
echo $post->link; // OR

Updating post custom fields:

$post = Post::find(1);
$post->meta->username = 'juniorgrossi';
$post->meta->url = 'http://grossi.io';
$post->save();

Inserting custom fields:

$post = new Post;
$post->save();

$post->meta->username = 'juniorgrossi';
$post->meta->url = 'http://grossi.io';
$post->save();

Advanced Custom Fields (ACF)

If you want to retrieve a custom field created by the Advanced Custom Fields (ACF) plugin, you have to install the corcel/acf plugin and call the custom field like this:

$post = Post::find(123);
echo $post->acf->some_radio_field;
$repeaterFields = $post->acf->my_repeater_name;

To avoid unnecessary SQL queries just set the field type you're requesting. Usually two SQL queries are necessary to get the field type, so if you want to specify it you're skipping those extra queries:

$post = Post::find(123);
echo $post->acf->text('text_field_name');
echo $post->acf->boolean('boolean_field_name');

Custom Post Type

You can work with custom post types too. You can use the type(string) method or create your own class.

// using type() method
$videos = Post::type('video')->status('publish')->get();

// using your own class
class Video extends Corcel\Post
{
    protected $postType = 'video';
}
$videos = Video::status('publish')->get();

Using type() method will make Corcel to return all objects as Corcel\Post. Using your custom class you have the advantage to customize classes, including custom methods and properties, return all objects as Video, for example.

Custom post types and meta data:

// Get 3 posts with custom post type (store) and show its address
$stores = Post::type('store')->status('publish')->take(3)->get();
foreach ($stores as $store) {
    $storeAddress = $store->address; // option 1
    $storeAddress = $store->meta->address; // option 2
    $storeAddress = $store->fields->address; // option 3
}

Single Table Inheritance

If you choose to create a new class for your custom post type, you can have this class be returned for all instances of that post type.

//all objects in the $videos Collection will be instances of Post
$videos = Post::type('video')->status('publish')->get();

// register the video custom post type and its particular class
Post::registerPostType('video', '\App\Video')


//now all objects in the $videos Collection will be instances of Video
$videos = Post::type('video')->status('publish')->get();

You can also do this for inbuilt classes, such as Page or Post. Simply register the Page or Post class with the associated post type string, and that object will be returned instead of the default one.

This is particular useful when you are intending to get a Collection of Posts of different types (e.g. when fetching the posts defined in a menu).

Shortcodes

You can add shortcodes by calling the addShortcode method on the Post model :

// [gallery id="1"]
Post::addShortcode('gallery', function ($shortcode) {
    return $shortcode->getName() . '.' . $shortcode->getParameter('id');
});
$post = Post::find(1);
echo $post->content;

If you are using Laravel, we suggest adding your shortcodes handlers in App\Providers\AppServiceProvider, in the boot method.

The thunderer/shortcode library is used to parse the shortcodes. For more information, click here.

Taxonomies

You can get taxonomies for a specific post like:

$post = Post::find(1);
$taxonomy = $post->taxonomies()->first();
echo $taxonomy->taxonomy;

Or you can search for posts using its taxonomies:

$post = Post::taxonomy('category', 'php')->first();

Post Format

You can also get the post format, like the WordPress function get_post_format():

echo $post->getFormat(); // should return something like 'video', etc

Pages

Pages are like custom post types. You can use Post::type('page') or the Page class.

// Find a page by slug
$page = Page::slug('about')->first(); // OR
$page = Post::type('page')->slug('about')->first();
echo $page->post_title;

Categories & Taxonomies

Get a category or taxonomy or load posts from a certain category. There are multiple ways to achieve it.

// all categories
$cat = Taxonomy::category()->slug('uncategorized')->posts()->first();
echo "<pre>"; print_r($cat->name); echo "</pre>";

// only all categories and posts connected with it
$cat = Taxonomy::where('taxonomy', 'category')->with('posts')->get();
$cat->each(function($category) {
    echo $category->name;
});

// clean and simple all posts from a category
$cat = Category::slug('uncategorized')->posts()->first();
$cat->posts->each(function($post) {
    echo $post->post_title;
});

Attachment and Revision

Getting the attachment and/or revision from a Post or Page.

$page = Page::slug('about')->with('attachment')->first();
// get feature image from page or post
print_r($page->attachment);

$post = Post::slug('test')->with('revision')->first();
// get all revisions from a post or page
print_r($post->revision);

Menu

To get a menu by its slug, use the syntax below. The menu items will be loaded in the nav_items variable. The currently supported menu items are: Pages, Posts, Links, Categories, Tags.

$menu = Menu::slug('primary')->first();

foreach ($menu->nav_items as $item) {
    // ....
    'post_title'    => '....', // Nav item name
    'post_name'     => '....', // Nav item slug
    'guid'          => '....', // Nav full url, influent by permalinks
    // ....
}

To handle multi-levels menus, loop through all the menu items to put them on the right levels in an array. Then, you can walk through the items recursively.

Here's just a basic example:

// first, set all menu items on their level
$menuArray = array();
foreach ($menu->nav_items as $item) {
    $parent_id = $item->meta->_menu_item_menu_item_parent;
    $menuArray[$parent_id][] = $item;
}

// now build the menu
foreach ($menuArray[0] as $item) {
    echo '.. menu item main ..';
    if (isset($menuArray[$item->ID])) {
        foreach($menuArray[$item->ID] as $subItem) {
            echo '.. show sub menu item ..';
        }
    }
}

Users

You can manipulate users in the same manner you work with posts:

// All users
$users = User::get();

// A specific user
$user = User::find(1);
echo $user->user_login;

Authentication

Using laravel

You will have to register Corcel's authentication service provider in config/app.php :

'providers' => [
    // Other Service Providers

    Corcel\Providers\Laravel\AuthServiceProvider::class,
],

And then, define the user provider in config/auth.php :

'providers' => [
    'users' => [
        'driver' => 'corcel',
        'model'  => Corcel\User::class,
    ],
],

To make Laravel's Password Reset work with Corcel, we have to override how passwords are stored in the database. To do this, you must change Auth/PasswordController.php from :

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;

class PasswordController extends Controller
{
    use ResetsPasswords;

to

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Corcel\Auth\ResetsPasswords as CorcelResetsPasswords;

class PasswordController extends Controller
{
    use ResetsPasswords, CorcelResetsPasswords {
        CorcelResetsPasswords::resetPassword insteadof ResetsPasswords;
    }

Using something else

You can use the AuthUserProvider class to authenticate an user :

$userProvider = new Corcel\Providers\AuthUserProvider;
$user = $userProvider->retrieveByCredentials(['username' => 'admin']);
if(!is_null($user) && $userProvider->validateCredentials($user, ['password' => 'admin'])) {
    // successfully login
}

Running tests

To run the phpunit tests, execute the following command :

./vendor/bin/phpunit

If you have the global phpunit command installed you can just type:

phpunit

Contributing

All contributions are welcome to help improve Corcel.

Before you submit your pull request consider the following guidelines:

  • Make your changes in a new git branch, based on the dev branch:

git checkout -b my-fix-branch dev

  • Create your patch/feature, including appropriate test cases.
  • Run the unit tests, and ensure that all tests pass.
  • In GitHub, send a pull request to corcel:dev.

Licence

MIT License Junior Grossi


  Classes of Junior Grossi  >  Corcel (Laravel + WordPress)  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog (1)  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: Corcel (Laravel + WordPress)
Base name: corcel
Description: Use the WordPress backend with any PHP application
Version: 1.0.31
PHP version: 5.4
License: MIT/X Consortium License
All time users: 364 users
All time rank: 6568
Week users: 10 users
Week rank: 73 Up
 
  Groups   Rate classes User ratings   Applications   Files Files  

  Groups  
Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes
Group folder image Databases Database management, accessing and searching View top rated classes
Group folder image Blogs Managing and publishing blogs View top rated classes


  Innovation Award  
PHP Programming Innovation award winner
May 2016
Winner


Prize: One big elePHPant Plush Mascott
WordPress is certainly one of the most popular and successful PHP based projects that was ever created apart of the PHP language itself.

However, great part of WordPress code is not even written using object oriented programming.

On the other hand Laravel is one of the most recent PHP frameworks that gained great traction supporting MVC and other design patterns.

This framework brings the world of WordPress to the Laravel developers, so you can write Laravel based applications with the WordPress backend.

Manuel Lemos

  User ratings  
RatingsUtility Consistency Documentation Examples Tests Videos Overall Rank
All time: Good (95%) Good (95%) Good (90%) - - - Sufficient (66%) 660
Month: Not enough user ratings

  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
File Role Description
Files folder imagemodels (8 files)
Files folder imagesrc (23 files, 6 directories)
Files folder imagetests (11 files, 1 directory)
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Class source

  Files folder image Files  /  models  
File Role Description
  Plain text file Category.php Class Class source
  Plain text file Comment.php Class Class source
  Plain text file Menu.php Class Class source
  Plain text file Options.php Class Class source
  Plain text file Page.php Class Class source
  Plain text file Post.php Class Class source
  Plain text file Taxonomy.php Class Class source
  Plain text file User.php Class Class source

  Files folder image Files  /  src  
File Role Description
Files folder imageAuth (1 file)
Files folder imageDatabase (1 file)
Files folder imageLaravel (2 files)
Files folder imagePassword (1 file)
Files folder imageProviders (1 file, 1 directory)
Files folder imageTraits (3 files)
  Plain text file Attachment.php Class Class source
  Plain text file Comment.php Class Class source
  Plain text file CommentBuilder.php Class Class source
  Plain text file Database.php Class Class source
  Plain text file Menu.php Class Class source
  Plain text file Model.php Class Class source
  Plain text file Options.php Class Class source
  Plain text file Page.php Class Class source
  Plain text file Post.php Class Class source
  Plain text file PostBuilder.php Class Class source
  Plain text file PostMeta.php Class Class source
  Plain text file PostMetaCollection.php Class Class source
  Plain text file Tag.php Class Class source
  Plain text file Term.php Class Class source
  Plain text file TermMeta.php Class Class source
  Plain text file TermRelationship.php Class Class source
  Plain text file TermTaxonomy.php Class Class source
  Plain text file TermTaxonomyBuilder.php Class Class source
  Plain text file ThumbnailMeta.php Class Class source
  Plain text file User.php Class Class source
  Plain text file UserBuilder.php Class Class source
  Plain text file UserMeta.php Class Class source
  Plain text file UserMetaCollection.php Class Class source

  Files folder image Files  /  src  /  Auth  
File Role Description
  Plain text file ResetsPasswords.php Class Class source

  Files folder image Files  /  src  /  Database  
File Role Description
  Plain text file Manager.php Class Class source

  Files folder image Files  /  src  /  Laravel  
File Role Description
  Accessible without login Plain text file config.php Aux. Auxiliary script
  Plain text file CorcelServiceProvider.php Class Class source

  Files folder image Files  /  src  /  Password  
File Role Description
  Plain text file PasswordService.php Class Class source

  Files folder image Files  /  src  /  Providers  
File Role Description
Files folder imageLaravel (1 file)
  Plain text file AuthUserProvider.php Class Class source

  Files folder image Files  /  src  /  Providers  /  Laravel  
File Role Description
  Plain text file AuthServiceProvider.php Class Class source

  Files folder image Files  /  src  /  Traits  
File Role Description
  Plain text file CreatedAtTrait.php Class Class source
  Plain text file HasAcfFields.php Class Class source
  Plain text file UpdatedAtTrait.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageconfig (2 files)
  Plain text file AuthenticationTest.php Class Class source
  Plain text file CommentTest.php Class Class source
  Plain text file HasMetaTest.php Class Class source
  Plain text file MenuTest.php Class Class source
  Plain text file OptionsTest.php Class Class source
  Plain text file PostMetaTest.php Class Class source
  Plain text file PostTest.php Class Class source
  Plain text file PostTypeTest.php Class Class source
  Plain text file TaxonomyTest.php Class Class source
  Plain text file TermTest.php Class Class source
  Plain text file UserTest.php Class Class source

  Files folder image Files  /  tests  /  config  
File Role Description
  Accessible without login Plain text file autoload.php Aux. Auxiliary script
  Accessible without login Plain text file database.sql Data Auxiliary data

Install with Composer Install with Composer - Download Download all files: corcel.tar.gz corcel.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.