WordPress, API, JSON – POC for a small Rest API with WordPress mobile applications
Still looking for a simple way to handle a POC to create a straightforward and simple API with a minimum of work.
I had 2 main objectives for this POC like in the previous post:
- This API should populate data for mobile application so I must be able to minimize/shrink the values exposed in the json output.
- As it is a POC, my purpose is to be totally autonomous. I do not want to rely on developers in the conception and creation of this API.
Why should I reinvent the wheel ? So why do not leverage on WordPress and especially since the version of WP 4.4, WP has a powerful API.
For those, who know already WP, I will just indicate what I will try to make the best of WP using : Post Type, Taxonomy, Custom Fields… etc.
I had in mind this time not an API to manage translations but an API to handle Urban Art Spots such as the website Urbacolors.
The WP REST API ecosystem
Here is the list of plugin that will enhance and ease your experience within the WP REST API.
I have tested them all, according to my needs, here is my feedback on each of them.
- These 3 plugins : REST API Log, WP Rest Routes, REST API Console are essential. If you want to work with the WP API, it is required.
- ACF to REST API: in my opinion, you’ll better use this plugin instead of ACF to WP API. It goes as a kind of “bundle” with the ACF (Advanced Custom Fields) Plugin for WordPress. Very powerful if you intend to use Custom Fields within your API.
- WP REST API: when you are using WP 4.4 and more, it is useless to install this plugin.
- WP REST API Cache: when you are in the development phase, it is useless to work with a cache because you can not see immediately the changes in JSON format for instance. In production, you can use it for sure.
Make you own plugin
The thing is that it is very easy to create your own plugin within WP especially if you want to use post_type. That is the best to do. It is a main purpose of my plug-in : managing 5 post_type with a custom and common taxonomy.
For some purpose, I did also some Custom Fields but believe me, better use the ACF plugin for POC at least and making Custom Fields, with the help of ACF (Advanced Custom Fields) plugin is a piece of cake.
I have made 2 “uncompleted” but sufficient plugins for the POC.
You can find on github @
Look for bf_poc_api_wp in https://github.com/bflaven/PluginWordpressForFun
- Urban Art Hunting App
- WP REST API Urban Art Hunting App
Few Web API Endpoint Reference for WP API
<!-- main Endpoint --> http://[path-to-your-worpress]/wp-json/wp/v2/ <!-- Posts Endpoint --> http://[path-to-your-worpress]/wp-json/wp/v2/posts <!-- etc... --> |
If you declare for each post_type 'show_in_rest' => true,
, automatically the WP API will show up your post_type.
You will get 2 routes with the same methods as you will have for the posts
For instance, our post_type is named locations
Show all the locations
wp/v2/locations ... methods": [ "GET", "POST" ], |
All methods for each locations record
wp/v2/locations/(?P<id>[\\d]+) ... "methods": [ "GET", "POST", "PUT", "PATCH", "DELETE" ], |
Some of the routes that automatically created by WP
Part 1 : The routes of the WordPress API
Part 2 : The routes of the WordPress API
REST API plugins
This Plugin WP Rest Routes is great. It will enable you to manage the routes of your API and the data displayed for each route. It is a true vital plugin.
In my opinion, the other 2 plugins (REST API Log, REST API Console) are useful but not vital.
Plugin WP Rest Routes – Part 1
Plugin WP Rest Routes – Part 2
Plugin WP Rest Routes – Part 3 – JSON output from the route get_locations
defined in the Plugin WP Rest Routes
The input of our previous screens capture inside the admin of WP, our post_type is named locations
The REST API Log useful to get data on traffic and on API’s usage
The REST API Console plug-in that simply list the endpoints. Not bad
Conclusion:
WP API has several advantages. I will stress 2 of them.
- Using WP gives the opportunity to anyone to create content that will be displayed in the API. It seems obvious but when you are working with people specialized for instance in Art, Literature or in any area. It is better to let them create the content to avoid any mistakes. Focus on the container and not on the content should be your motto.
- With the help of the Plugin WP Rest Routes, you have the ability to design the architecture quickly with all the routes of your future API. Thus, it will give you a living example for your future development with more experienced developers. According to the proverb : “A picture is worth a thousand words”.
Learn More
- wp-api-menus from Fulvio Notarstefano
https://github.com/unfulvio/wp-api-menus/tree/master/includes - Leveraging the WordPress REST API
https://cnpagency.com/blog/leveraging-the-wordpress-rest-api/ - WP REST API Part 2: Customizing Default Endpoints and Adding New Ones
https://deliciousbrains.com/wp-rest-api-customizing-endpoints-adding-new-ones/ - Adding custom routes to the WordPress REST API
https://torquemag.io/2015/06/adding-custom-routes-wordpress-rest-api/ - WP API: Adding Custom Endpoints
https://webdevstudios.com/2016/05/24/
wp-api-adding-custom-endpoints/ - How to: create a custom endpoint with the core REST API in WordPress
http://wpcode.io/create-custom-endpoint-core-rest-api-wordpress/ - Adding fields to the JSON response + Removing fields from the JSON response
https://css-tricks.com/using-the-wp-api-to-fetch-posts/ - My best practices to developing with WP-API
https://www.roysivan.com/my-best-practices-to-developing-wp-api/ - Unset data from WordPress API (wp-json)
https://stackoverflow.com/questions/32511201/unset-data-from-wordpress-api-wp-json - REST API Usage Example
https://generatewp.com/snippet/pNkKpVE/ - Working with post meta data using the WordPress REST API
https://torquemag.io/2015/07/working-with-post-meta-data-using-the-wordpress-rest-api/ - Excellent post from Basememara – Full Stack iOS and WordPress in Swift
http://basememara.com/full-stack-ios-and-wordpress-in-swift/ - Compatible Plugins & Tools from WP REST API
http://v2.wp-api.org/guide/plugins/ - Custom post types in WordPress
http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress - Plugin Advanced Custom Fields
https://wordpress.org/plugins/advanced-custom-fields/ - Adding Custom Fields to a Custom Post Type, the Right Way
http://blog.teamtreehouse.com/adding-custom-fields-to-a-custom-post-type-the-right-way
- WordPress custom post types and advanced custom fields
https://www.freshconsulting.com/wordpress-custom-post-types-advanced-custom-fields/ - Ultimate Guide to Custom Post Types in WordPress
https://typerocket.com/ultimate-guide-to-custom-post-types-in-wordpress/ - Unable to save custom taxonomy terms in a custom-built metabox from stackexchange
https://wordpress.stackexchange.com/questions/9015/unable-to-save-custom-taxonomy-terms-in-a-custom-built-metabox - Saving Taxonomy Terms from stackexchange
https://wordpress.stackexchange.com/questions/1794/saving-taxonomy-terms - The Custom Post Types Guide for WordPress
https://www.wpmayor.com/ultimate-guide-wordpress-custom-post-types/ - WordPress Taxonomies Extra Fields the easy way
https://en.bainternet.info/wordpress-taxonomies-extra-fields-the-easy-way/ - Add a Metabox to Your Custom Post Type Screen
http://shibashake.com/wordpress-theme/add-metabox-custom-post-type - WordPress Meta Boxes
https://rudrastyh.com/wordpress/meta-boxes.html - How To Create WordPress Meta Boxes
https://www.smashingmagazine.com/2011/10/create-custom-post-meta-boxes-wordpress/ - Create custom taxonomy and Display in metabox dropdown from stackexchange
https://wordpress.stackexchange.com/questions/80059/create-custom-taxonomy-and-display-in-metabox-dropdown - Code add-metabox-to-taxonomy.php
https://gist.github.com/ms-studio/543a0f7dd8ac05ccf037 - How I add a WordPress MetaBox
https://en.bainternet.info/how-i-add-a-wordpress-metabox/ - My WordPress Meta box Class from bainternet
https://github.com/bainternet - How To Create A Better Meta Box In WordPress Post Editing Page
http://deluxeblogtips.com/how-to-create-meta-box-wordpress-post/ - WordPress Meta Boxes: a Comprehensive Developer’s Guide
https://themefoundation.com/wordpress-meta-boxes-guide/ - Add special meta box to custom post type from Stackexchange
https://wordpress.stackexchange.com/questions/186026/add-special-meta-box-to-custom-post-type - WordPress Meta Box for custom post type from stackoverflow
https://stackoverflow.com/questions/24587751/wordpress-meta-box-for-custom-post-type - Adding a custom post type with custom meta box in wordpress
https://return-true.com/adding-custom-post-type-and-custom-meta-box-in-wordpress/ - Creating Post Type Relationships
https://wp-types.com/documentation/user-guides/creating-post-type-relationships/ - The Practical Guide to Multiple Relationships Between Posts in WordPress
https://onextrapixel.com/the-practical-guide-to-multiple-relationships-between-posts-in-wordpress/ - Relationships between Post Types in WordPress
http://jancbeck.com/articles/relationships-between-wordpress-post-types - Relation Post Types : Faire des relations entre les contenus de WordPress !
http://www.herewithme.fr/2010/08/16/relation-post-types-faire-des-relations-entre-les-contenus-de-wordpress/ - Post relationships: Parent-to-child
http://justintadlock.com/archives/2013/10/07/post-relationships-parent-to-child - Part 9 – Many-to-many relationships in Toolset
https://wp-types.com/documentation/toolset-training-course/part-9-many-to-many-relationships-in-toolset/ - Many to Many Relationship between Two Custom Post Types
https://wordpress.stackexchange.com/questions/51386/many-to-many-relationship-between-two-custom-post-types - One-to-many Relationships in WordPress
https://codeseekah.com/2012/02/14/one-to-many-relationships-in-wordpress/ - Understanding and Working with Relationships Between Data in WordPress
https://code.tutsplus.com/tutorials/understanding-and-working-with-relationships-between-data-in-wordpress–cms-20632 - Create many-to-many relationships between posts of any type using post 2 post plugin
https://premium.wpmudev.org/forums/topic/create-many-to-many-relationships-between-posts-of-any-type-using-post-2-post-plugin - Using 1 taxonomy for multiple post types? from stackexchange
https://wordpress.stackexchange.com/questions/86864/using-1-taxonomy-for-multiple-post-types - Add Exactly The Same Taxonomies To Multiple Custom Post Types
https://wpsites.net/wordpress-admin/add-exactly-the-same-taxonomies-to-mutiple-custom-post-types/ - Extending the Quick Edit Tool for Taxonomy Terms
https://code.tutsplus.com/articles/extending-the-quick-edit-tool-for-taxonomy-terms–wp-20495 - WordPress post to post relationships, without altering the Database, excellent post on one-to-many in WP
http://www.danielauener.com/wordpress-post-to-post-relationships-without-altering-the-database/ - Creating Many to Many Relationships Between Posts in WordPress
https://www.never5.com/blog/creating-many-many-relationships-posts-wordpress/ - WordPress, custom post type relationship to many post from stackoverflow
https://stackoverflow.com/questions/31308217/wordpress-custom-post-type-relationship-to-many-post - Relating Posts, Pages, and Custom Post Types in WordPress
https://www.webmalama.com/relating-posts-pages-custom-post-types-wordpress/ - How to set parent-child relationship between differents custom post types
https://wordpress.stackexchange.com/questions/181134/how-to-set-parent-child-relationship-between-differents-custom-post-types - Extending WordPress With Custom Content Types
https://www.smashingmagazine.com/2015/04/extending-wordpress-custom-content-types/ - How to Use Radio Buttons With Taxonomies
https://code.tutsplus.com/articles/how-to-use-radio-buttons-with-taxonomies–wp-24779 - WordPress: Custom post types and parent-child relationships
http://typecode.digital/wordpress-custom-post-types-and-parent-child-relationships/ - Plugin Custom Post Type UI
https://wordpress.org/plugins/custom-post-type-ui/ - Plugin Posts 2 Posts
https://wordpress.org/plugins/posts-to-posts/ - Posts 2 Posts, un plugin pour créer des relations entre les types de contenus de WordPress
https://wpchannel.com/posts-2-posts-plugin-creer-relations-custom-post-types/ - Personnaliser l’icône d’un custom post type avec la police dashicons
http://www.geekpress.fr/personnaliser-licone-dun-custom-post-type-police-dashicon/ - How To Design And Style Your WordPress Plugin Admin Panel
https://onextrapixel.com/how-to-design-and-style-your-wordpress-plugin-admin-panel/
API WP
Custom Post Types, Custom Fields & Custom Taxonomy
Create a Meta Box in WP
Parent Child Relationships issue
Plugins
Other Resources