Currently only supports GET requests.
TABLE OF CONTENTS:
Install via composer:
composer require maneuver/channel
And include the autoloader:
require('vendor/autoload.php');
Happy times. 🤙
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',
]);
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',
]);
Currently not implemented.
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>
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;
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
Get all users:
$users = $channel->getUsers();
echo $users[0]->name;
Get all media:
$media = $channel->getMedia();
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.
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
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.
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'
- More support for ACF fields
- Better support for images
- Add WP_Query-like parameters
- OAuth authentication