Output JSON with Pods
Pods is a great framework for WordPress. It enables you to create your data structures with basic point and click actions, transforming WordPress into a really easy to enhance CMS system. Pods pages with the permalink URL structure also allow you to code your own little JSON endpoints for a Frontend app. You might to want such methods for example to omit your micro caching solution for repeating content on pages – e.g. latest tweets, without using server-side includes. Another application would be to use WordPress as a Web Service and communicate with a 3rd part website, app or client Frontend through such.
So let's create a basic pods page named car/*
which will listen to any url that has cars/ in it, and pass on the value as a parameter.
We'll then output the content of the item that matches the url variable, and throw it out as JSON for some client side Javascript to consume. We'll use Pod's pods->export()
feature, which is actually mapped from pods_api->export()
, but can be used with pods as well. see the pods->export() documentation
Here's our code for a single Pods item returned in JSON :
<?php
/*
Template Name: json output for cars
*/
$car = pods('cars',pods_var('last', 'url'));
$car_pod = $car->export();
if ( !empty( $car_pod ) ) {
die(json_encode($car_pod));
}else{
die(json_encode(array('error' => 'Sorry, no such car exists.')));
}
?>
Save this file in your theme directory and in your pods page select 'json output for cars' as the template file. Now any request to e.g. yourblog.com/car/mitsubishi
will look up the pod with the slug or id matching mitsubishi
and output its' JSON equivalent data.
If you would like to output all of your pods entries, you'd want to use the export_data()
function. see the pods->export_data() documentation
So for this, we would create the pods page cars
or cars/all
, depending on your structure and attach the following template file :
<?php
/*
Template Name: json output for all cars
*/
$cars = pods('cars', array('orderby' => 'name asc', 'limit' => -1));
$all_cars = $cars->export_data();
if ( !empty( $all_cars ) ) {
die(json_encode($all_cars));
}else{
die(json_encode(array('error' => 'No cars found.')));
}
?>
You could of course also use one template file for both, and switch what you're using based on if you've received a url var, but this could be tricky based on the use case.
Another use would be to only list cars based on the manufacturer you provided in the url. So let's say we create the pods page called cars_by_manufacturer/*
with the following code :
<?php
/*
Template Name: json output for all cars by manufacturer
*/
// Let's check if the manufacturer was sent as slug, or as ID and create a variable for the id based on that
if(!ctype_digit(pods_var('last','url'))){
$manufacturer = pods('manufacturers',pods_var('last', 'url'));
$manufacturer_id = $manufacturer->raw('id', true);
}else{
$manufacturer_id = pods_var('last','url');
}
$cars = pods('cars', array('orderby' => 'name asc', 'limit' => -1, 'where' => 'manufacturer.ID="'.$manufacturer_id.'"'));
$cars_by_manufacturer = $cars->export_data();
if ( !empty( $cars_by_manufacturer ) ) {
die(json_encode($cars_by_manufacturer ));
}else{
die(json_encode(array('error' => 'No cars found for this manufacturer.')));
}
?>
You might want to add some basic access permissions checks, or even build your own REST endpoint with POST/GET/PUT/DELETE handling from here, and hook it up to your Backbone or Angular Javascript MVC app with a WordPress Backend. This is extremely nice to quickly bootstrap apps with an easy to use backend interface that people are familiar with and can start working in immediately.
And this would conclude a basic JSON endpoint with Pods Framework in WordPress, enjoy!
Comment
by George Fardellas
Hi! Thanks for this tutorial!
I am newbie in Pods and I am trying to implement a dummy mobile app (with phonegap or titanium) which will have a form for creating a post. For example create a post with title, desc. and upload some photos.
Is your theory with JSON and REST, applicable to my plans?
Do you in mind any consideration about my mini-project?
I know that WP-Api is released officially now.
Do you have any example from your own experience that it could be helpful for me?
Thanks a lot for your time and have a nice day!!
May 17, 2015