EmberSherpa / wp-rest-jsonapi-serializer

16 stars 1 forks source link

JSONAPI Serializer for WP REST API v2 & Pods - WordPress Plugin

This is a plugin for WordPress that'll make your site's WP REST API generate JSONAPI payload. This plugin was created to make it easier to use Ember.js and Ember Data as an API client for WordPress.

Warning: Pods are probably required at this point

Features

Not Supported

Ember Data setup

  1. Configure Adapter

Create adapters/application.js and specify your WordPress' host.

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
      host: '<your_wp_host>',
      namespace: 'wp-json/wp/v2'
});
  1. Create models

Post, Tag, Category, Author, Taxonomy

// TODO: create an Ember Addon to provide these defaults

Using Slugs

Configure your route accept a slug:

// router.js

import Ember from 'ember';
import config from './config/environment';

const Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
    this.route('post', { path: 'posts/:slug' };
});

export default Router;
// routes/posts.js

import Ember from 'ember';

export default Ember.Route.extend({

  model({slug}) {
    return this.store.queryRecord('post', {
      filter: { name: slug }
    });
  }

});

CORS

You'll need to let WordPress accept API requests from localhost:4200. We're allowing API calls from all hosts by adding the following code via theme's functions.php. You may choose to do the same at your discression.

<?php
add_filter('http_origin', 'http_origin_set_origin_to_all');
function http_origin_set_origin_to_all() {
        return $_SERVER[ 'HTTP_ORIGIN' ];
}