funkhaus / Rest-Easy

Wordpress plugin to rest-ify your site with zero effort and powerful customization.
14 stars 1 forks source link

Rest-Easy is a Wordpress plugin designed to Rest-ify your site with zero effort and powerful customization.

Table of Contents

  1. Installation
  2. Tutorial
    1. Basics
    2. Custom Filters
  3. Core Concepts
  4. Reference
    1. Filters
      1. Builder Filters
        1. Site Data
        2. Meta Data
        3. Loop Data
      2. Serializer Filters
        1. Serialize Object
        2. Serialize Attachment
        3. Serialize Menu
        4. Serialize Nav Item
        5. Serialize Post
        6. Gather Related
    2. Utility Functions
    3. Integrations

Installation

Rest-Easy was built as a companion to Vuepress, so if you're using VP, Rest-Easy will be installed automatically.

If you're not using Vuepress, or would otherwise like to install the plugin manually, follow these steps:

  1. Download the latest version of the plugin.
  2. Go to your site's plugin installation page ([your-site.com]/wp-admin/plugin-install.php).
  3. Click "Upload Plugin," then upload the .zip file from step 1.
  4. Go to your site's Plugins page ([your-site.com]/wp-admin/plugins.php) then click "Activate" on the Rest Easy plugin.

That's it!

Tutorial

Take a look at the visual docs to see a step-by-step roadmap of a Rest-Easy response. If you're using Vuepress, the Basics section is handled automatically - you can continue reading for an idea of how Rest-Easy works, or head down to Custom Filters for the next steps.

Basics

Once you've installed Rest-Easy, navigate to any page on your site with ?contentType=json added at the end of the URL.

You'll see a serialized JSON object with the data of the page you requested - a lightweight and thorough summary of the current page, with zero setup on your end!

To fetch this JSON-serialized version of a page programmatically, you can do something like this in your site's JS:

fetch(myUrl + '?contentType=json')
    .then(res => {
        return res.json()
    })
    .then(json => console.log(json))

This example fetches the requested page of your site and returns the same JSON object that you got with the ?contentType=json query parameter. Right away, you've got a working RESTful API with plenty of detailed information at your disposal.

Diagram showing flow of Rest-Easy data construction

Custom Filters

Rest-Easy makes some assumptions about how you'd want a page to be serialized - but what if you want to change that serialization?

Let's say you want to have a custom field called _my_custom_field that you want to make available in the data. Add the following to your theme's functions.php file:

function add_custom_field($input) {
    global $post;
    $input['myCustomField'] = $post->_my_custom_field;

    return $input;
}
add_filter('rez_serialize_post', 'add_custom_field');

Let's go through that line-by-line.

Now, whenever you load a post with _my_custom_field defined, you'll see your custom field in jsonData.loop[0]._my_custom_field!

Core Concepts

(This is under-the-hood information - don't stress about this if you're not actually developing Rest-Easy!)

To avoid infinite loops in page serialization, Rest-Easy uses two main concepts: builders and serializers.

A builder will run once on a page. It combines the output of several serializers and returns that data as an associative array, which is then JSON-encoded to form jsonData.

(Note that builders are also the only functions that can gather related posts - if anything else could do so, related posts would keep building on top of themselves without staying one level deep like they do in the Loop builder.)

A serializer will take one piece of data from Wordpress and translate it into an associative array. For example, a serializer will take a post and turn it into an array with that post's title, content, permalink, and so on.

Rest-Easy's entry point is rest-easy.php, where it:

  1. Runs the serializers in builders.php's rez_build_all_data function
  2. Determines how to send the requested output to the user by:
    • checking the request's CONTENT_TYPE and query strings for a JSON request, echoing the jsonData object if one was found
    • dumping the jsonData object onto the page with wp_localize_script otherwise

Reference

Filters

Add custom filters to build your own data:

function custom_function_name($input){
    // modify $input here...
    return $input;
}
add_filter('desired_rest_easy_filter', 'custom_function_name');

Default values are shown below.

Builder Filters

Builders run once per page. They're designed to collect serialized data, add some high-level site/meta information, and output the resulting JSON object. Most of the time, you'll only use builders when adding very general site information - region detecting, custom site-wide taglines, etc.

Site Data
Meta Data
Loop Data

Serializer Filters

Serializers are designed to take any WordPress object and translate it into JSON data. Serializers should be customized when you want to change the information that comes back from a single post, page, media item, etc. Post authors, media upload dates, and custom meta fields are great candidates for custom serializers.

Serialize Object
Serialize Attachment
Serialize Menu
Serialize Nav Item
Serialize Post
Gather Related

Utility functions

Integrations

Rest-Easy is built to work well with other Funkhaus plugins:


Rest-Easy

http://funkhaus.us