2017 © Pedro Peláez
 

library channel

PHP library for the Wordpress REST API

image

maneuver/channel

PHP library for the Wordpress REST API

  • Monday, October 30, 2017
  • by maneuver
  • Repository
  • 2 Watchers
  • 0 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 8 % Grown

The README.md

PHP library for the Wordpress REST API

Currently only supports GET requests., (*1)


TABLE OF CONTENTS:, (*2)


Installation

Install via composer:, (*3)

composer require maneuver/channel

And include the autoloader:, (*4)

require('vendor/autoload.php');

Happy times. 🤙, (*5)

Authentication

Basic Authentication

Make sure the Basic Authentication plugin for Wordpress is installed and activated.
(should only be used for development purposes, as stated by the repository), (*6)

$channel = new \Maneuver\Channel([
  'uri' => 'http://example.com/wp-json/wp/v2/',
  'username' => 'your-username',
  'password' => 'your-password',
]);

API Token

Make sure the Rooftop API Authentication plugin is installed and activated., (*7)

$channel = new \Maneuver\Channel([
  'uri' => 'http://example.com/wp-json/wp/v2/',
  'token' => 'your-token',
]);

OAuth

Currently not implemented., (*8)

Usage

Posts

Retrieve a list of all posts (where post_type = 'post'):, (*9)

$posts = $channel->getPosts();

echo count($posts);

Retrieve a post by ID:, (*10)

$post = $channel->getPost(1);

echo $post->excerpt;

Using Twig? Fear not:, (*11)

<h1>{{ post.title }}</h1>
<p>{{ post.excerpt|raw }}</p>

Pages

Retrieve a list of all pages:, (*12)

$pages = $channel->getPages();

foreach ($pages as $page) {
  echo $page->title;
}

Retrieve a page by ID:, (*13)

$page = $channel->getPage(1);

echo $page->content;

Taxonomies & Terms

Retrieve all existing taxonomies:, (*14)

$taxonomies = $channel->getTaxonomies();

Retrieve one taxonomy by slug:, (*15)

$taxonomy = $channel->getTaxonomy('category'); // use singular taxonomy name

// Then you can retrieve its terms:
$terms = $taxonomy->terms();

Or retrieve the terms in one call using the 'get' method:, (*16)

$terms = $channel->get('categories'); // use plural taxonomy name

Users

Get all users:, (*17)

$users = $channel->getUsers();

echo $users[0]->name;

Media

Get all media:, (*18)

$media = $channel->getMedia();

Custom Post Types

When you define a custom post type in your Wordpress installation, make sure you set the show_in_rest option to true. This exposes an endpoint in the REST API to retrieve the posts. Read the docs, (*19)

add_action('init', function(){

  register_post_type('product', [
    'labels' => [
      'name'          => 'Products',
      'singular_name' => 'Product',
    ],
    'public'        => true,
    'show_in_rest'  => true,
    'rest_base'     => 'products' // defaults to the post type slug, 'product' in this case
  ]);

});

Then use the general 'get' method:, (*20)

$products = $channel->get('products'); // Pass in the 'rest_base' of the custom post type.

Slightly more advanced stuff

Endpoints

You can actually call any endpoint using the 'get' method:, (*21)

$post_types = $channel->get('types');
$latest = $channel->get('posts?per_page=5');

Read more about all endpoints in the REST API Handbook, (*22)

Guzzle

You can pass in more requestOptions for Guzzle:, (*23)

$latest = $channel->get('posts?per_page=5', [
  'proxy' => 'tcp://localhost:8125',
]);

Read more about the Guzzle RequestOptions here., (*24)

Custom Classes

Every call returns an object (or array of objects) extending the '\Maneuver\Models\Base' class. You can define your own classes if needed., (*25)

NOTE: Don't extend the '\Maneuver\Models\Base' class directly, you'll lose some functionality., (*26)

class MyPost extends \Maneuver\Models\Post {
  public function fullTitle() {
    return 'Post: ' . $this->title;
  }
}

class MyPage extends \Maneuver\Models\Page {

}

$channel->setCustomClasses([
  // 'type' => 'ClassName'
  // eg: 'user' => 'MyUser'
  // or:
  'post'    => 'MyPost',
  'page'    => 'MyPage',
  'product' => 'MyPost', // custom post type
]);

$post = $channel->getPost(1);

echo $post->fullTitle();

echo get_class($post);
// => 'MyPost'

Todo:

  • More support for ACF fields
  • Better support for images
  • Add WP_Query-like parameters
  • OAuth authentication

The Versions

30/10 2017

dev-master

9999999-dev

PHP library for the Wordpress REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by David De Coninck

wordpress api php rest api

30/10 2017

v0.0.2

0.0.2.0

PHP library for the Wordpress REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by David De Coninck

wordpress api php rest api

05/05 2017

v0.0.1

0.0.1.0

PHP library for the Wordpress REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by David De Coninck

wordpress api php rest api