All Projects → oskarrough → Ember Wordpress

oskarrough / Ember Wordpress

Licence: mit
The bridge between Ember.js and Wordpress

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Ember Wordpress

Better Rest Endpoints
A WordPress plugin that serves up slimmer WP Rest API endpoints.
Stars: ✭ 56 (-40.43%)
Mutual labels:  wordpress-api, wordpress
Ember Styleguide
This is a UI addon that intends to help standardize the Ember family of websites and make it easier to make the Ember website an Ember app.
Stars: ✭ 69 (-26.6%)
Mutual labels:  ember, ember-addon
Ember Side Menu
Side menu component for Ember.js applications
Stars: ✭ 58 (-38.3%)
Mutual labels:  ember, ember-addon
Headless Wp
A demo repo for Headless WordPress
Stars: ✭ 89 (-5.32%)
Mutual labels:  wordpress-api, wordpress
Ember Cli Sentry
Error tracking via Sentry for Ember.js apps
Stars: ✭ 81 (-13.83%)
Mutual labels:  ember, ember-addon
Virtual Each
Ember infinite list component, inspired by react-infinite-list
Stars: ✭ 51 (-45.74%)
Mutual labels:  ember, ember-addon
Ember Cli Foundation 6 Sass
Stars: ✭ 65 (-30.85%)
Mutual labels:  ember, ember-addon
Ember React Components
Render React components in Ember
Stars: ✭ 43 (-54.26%)
Mutual labels:  ember, ember-addon
Ember Cli Bundle Analyzer
Analyze the size and contents of your Ember app's bundles
Stars: ✭ 78 (-17.02%)
Mutual labels:  ember, ember-addon
Ember Cli Coffeescript
Adds precompilation of CoffeeScript files and all the basic generation types to the ember generate command.
Stars: ✭ 72 (-23.4%)
Mutual labels:  ember, ember-addon
Ember Sticky Element
An ember component that mimics the functionality of `position: sticky`
Stars: ✭ 51 (-45.74%)
Mutual labels:  ember, ember-addon
Ember Steps
Declaratively create wizards, tabbed UIs, and more
Stars: ✭ 84 (-10.64%)
Mutual labels:  ember, ember-addon
Docfy
Build fully personalized documentation sites; write content and demos in Markdown.
Stars: ✭ 48 (-48.94%)
Mutual labels:  ember, ember-addon
Ember Simple Auth Auth0
Auth0 + lock.js, built on ember-simple-auth
Stars: ✭ 53 (-43.62%)
Mutual labels:  ember, ember-addon
Hybrid
[I don't have time to work on this anymore. Use at your own risk] Build WordPress based PWA, iOS, Android & Windows phones apps in minutes!
Stars: ✭ 1,026 (+991.49%)
Mutual labels:  wordpress-api, wordpress
Ember Cli Chartist
Ember Addon for Chartist.js
Stars: ✭ 58 (-38.3%)
Mutual labels:  ember, ember-addon
Ember Accessibility
An EmberJS addon to help identify accessibility violations during development
Stars: ✭ 29 (-69.15%)
Mutual labels:  ember, ember-addon
Ultimate Fields
The plugin for custom fields in WordPress
Stars: ✭ 39 (-58.51%)
Mutual labels:  wordpress-api, wordpress
Ember Cli Clipboard
A simple ember wrapper around clipboard.js
Stars: ✭ 72 (-23.4%)
Mutual labels:  ember, ember-addon
Ember Drag Sort
A sortable list component with support for multiple and nested lists
Stars: ✭ 82 (-12.77%)
Mutual labels:  ember, ember-addon

Ember Wordpress

Ember Wordpress is an addon for ember-cli that makes it easy to fetch data from the Wordpress API (WP-API) in your Ember sites. It includes an application adapter, serializer as well as some default models: post, page, category and tag.

Demonstration

Note, the demo API sometimes goes to sleep. Please open an issue if so.

A few sites using ember-wordpress:

How to install

  1. Run ember install ember-wordpress
  2. Define where your Wordpress installation is:
// config/environment.js
...
ENV.emberWordpress: {
  host: 'https://my-wordpress-site.com'
}

On a version before 2.0.1? Use ENV.wordpressHost instead.

Models

You'll have seven models ready out of the box: wordpress/post, wordpress/page, wordpress/category wordpress/tag, wordpress/attachment, wordpress/comment, wordpress/user.

Note: the wordpress/post and wordpress/page models are identical and so are wordpress/category and wordpress/tag. For your own custom post types, it is recommended to extend the post model:

// app/models/recipe.js
import DS from 'ember-data';
import PostModel from 'ember-wordpress/models/post';
export default PostModel.extend({
  ingredients: DS.attr()
});

If you're using the ACF plugin your custom fields will be at model.get('acf.myCustomField').

Configuring Wordpress

Since Wordpress version 4.7 the REST API is included in core Wordpress. If you are on an earlier version you will need to install the WP API v2 plugin, which also works fine.

After installing, create some posts or pages in Wordpress and see your data at example.com/wp-json/wp/v2.

If you're having CORS trouble: WP-CORS If you want custom fields: Advanced Custom Fields and ACF To REST API

Wordpress custom post types

To use a custom post type together with the WP API you have to be aware of two additional arguments, when you define them.

  1. show_in_rest must be set to true.
  2. rest_base will be the endpoint of your post type. Sset it to the plural form of your model, as this is what Ember expects. E.g. the endpoint for a recipe post type should be recipies and not recipe.

Here's a full example. You could save this file as wp-content/plugins/my-custom-post-types.php.

<?php
/*
Plugin Name: My custom post types
Author URI: https://github.com/oskarrough/ember-wordpress/
*/
function artist_post_type() {
	$labels = array(
		'name' => 'Artists',
		'singular_name' => 'Artist',
		'menu_name' => 'Artists',
	);
	$args = array(
		'labels' => $labels,
		'show_in_rest'	=> true,
		'rest_base' => 'artists',
	);
	register_post_type('artist', $args);
}

add_action('init', 'artist_post_type');
?>

Queries

The WP API supports many arguments that you can use but it's not super friendly so here are some tips.

How to query more than 10 items

By default the WP API returns a maximum of 10 items. For instance, this.store.findAll('post') would return 10 posts. To change that we need to find the right argument and endpoint. Looking at the documentation for WP API we find per_page. It could look like wp-json/wp/v2/posts?per_page=99 which translates into the ember-data query this.store.query('post', {per_page: 99}).

How to query by slug

  • Endpoint: wp-json/wp/v2/posts?slug=some-post-slug
  • Query: this.store.query('post', {slug: 'some-post-plug'}).then(models => models.get('firstObject'));

We take the first object because query always returns an array and we expect our query to only return a single object.

How to query by category

To query posts by category slug you will need two queries.

First get the category id with the

  • Endpoint: wp-json/wp/v2/categories?slug=some-category-slug
  • Query: this.store.query('category', {slug: 'some-category-slug'}).then(models => models.get('firstObject'));

Then, get the posts

  • Endpoint: wp-json/wp/v2/posts?categories=category-id&per_page=99
  • Query: this.store.query('post', {per_page: 99, categories: category-id}).then(models => models.get('firstObject'));

How to enable caching for the WP API

Enable caching by installing the wp-rest-api-cache wordpress plugin.

Server-side rendering with FastBoot

To get server-side rendering, install Ember Fastboot. Here's a demo of the Ember Wordpress dummy app served by fastboot. You'll see the actual HTML rendered if you view the source. Ember Wordpress doesn't require anything special to make this work. Here's a small deployment tip.

Eager loading

By default, Ember loads every request to a record separately from the server. If you want to display a post and the names of all of it's tags for example, Ember will query the main post and every single tag. A post with five tags will result in six requests to the server.

Since Ember and WP-API supports loading of multiple resources of the same type in one request, you can opt-in to this feature:

var ENV = {
  ...
  emberWordpress: {
    coalesceFindRequests: true
  }  
  ...

With this option enabled, loading a post with five tags will result in just two requests, because all tags of the post will be loaded together. This can improve the load time of your Ember app a lot!

Contributing

It's the goal of ember-wordpress to be the bridge between ember/ember-data and the official WP REST API. Ideally, in addition to the provided adapter, serializer and models, this readme and the project's demo app should serve as good examples. Please ask any questions here https://github.com/oskarrough/ember-wordpress/issues.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].