Skip to content

maneuver-agency/channel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP library for the Wordpress REST API

Currently only supports GET requests.


TABLE OF CONTENTS:


Installation

Install via composer:

composer require maneuver/channel

And include the autoloader:

require('vendor/autoload.php');

Happy times. 🤙

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)

$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.

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

OAuth

Currently not implemented.

Usage

Posts

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

$posts = $channel->getPosts();

echo count($posts);

Retrieve a post by ID:

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

echo $post->excerpt;

Using Twig? Fear not:

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

Pages

Retrieve a list of all pages:

$pages = $channel->getPages();

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

Retrieve a page by ID:

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

echo $page->content;

Taxonomies & Terms

Retrieve all existing taxonomies:

$taxonomies = $channel->getTaxonomies();

Retrieve one taxonomy by slug:

$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:

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

Users

Get all users:

$users = $channel->getUsers();

echo $users[0]->name;

Media

Get all media:

$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

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:

$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:

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

Read more about all endpoints in the REST API Handbook

Guzzle

You can pass in more requestOptions for Guzzle:

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

Read more about the Guzzle RequestOptions here.

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.

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

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

About

PHP library for the Wordpress REST API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages