Skip to content

Instantly share code, notes, and snippets.

@jimakker
Created August 22, 2012 01:02
Show Gist options
  • Save jimakker/3421074 to your computer and use it in GitHub Desktop.
Save jimakker/3421074 to your computer and use it in GitHub Desktop.
Add custom fields capability to JSON-feed Wordpress plugin
<?php
/*
Plugin Name: JSON feed
Plugin URI: http://wordpress.org/extend/plugins/json-feed/
Description: Provides feeds in JSON form
Version: 1.3
Author: Chris Northwood (modified by Dan Phiffer)
Author URI: http://www.pling.org.uk/
Contributors: @jimakker
Comments: works on WP 3.4.1
*/
add_filter('query_vars', 'json_feed_queryvars');
function json_feed_queryvars($qvars)
{
$qvars[] = 'jsonp';
$qvars[] = 'custom';
$qvars[] = 'date_format';
$qvars[] = 'remove_uncategorized';
return $qvars;
}
function json_feed()
{
$output = array();
while (have_posts())
{
the_post();
$output[] = array
(
'id' => (int) get_the_ID(),
'permalink' => get_permalink(),
'title' => get_the_title(),
'content' => get_the_content(),
'excerpt' => get_the_excerpt(),
'date' => get_the_time(json_feed_date_format()),
'categories' => json_feed_categories(),
'tags' => json_feed_tags(),
'custom' => json_feed_custom_vars(get_post_custom())
);
}
if (get_query_var('jsonp') == '')
{
header('Content-Type: application/json; charset=' . get_option('blog_charset'), true);
echo json_encode($output);
}
else
{
header('Content-Type: application/javascript; charset=' . get_option('blog_charset'), true);
echo get_query_var('jsonp') . '(' . json_encode($output) . ')';
}
}
function json_feed_date_format()
{
if (get_query_var('date_format'))
{
return get_query_var('date_format');
}
else
{
return 'F j, Y H:i';
}
}
function json_feed_categories()
{
$categories = get_the_category();
if (is_array($categories))
{
$categories = array_values($categories);
if (get_query_var('remove_uncategorized'))
{
$categories = array_filter($categories, 'json_feed_remove_uncategorized');
}
return array_map('json_feed_format_category', $categories);
}
else
{
return array();
}
}
function json_feed_remove_uncategorized($category)
{
if ($category->cat_ID == 1 && $category->slug == 'uncategorized')
{
return false;
}
else
{
return true;
}
}
function json_feed_format_category($category)
{
return array
(
'id' => (int) $category->cat_ID,
'title' => $category->cat_name,
'slug' => $category->slug
);
}
function json_feed_tags()
{
$tags = get_the_tags();
if (is_array($tags))
{
$tags = array_values($tags);
return array_map('json_feed_format_tag', $tags);
}
else
{
return array();
}
}
function json_feed_format_tag($tag)
{
return array
(
'id' => (int) $tag->term_id,
'title' => $tag->name,
'slug' => $tag->slug
);
}
// Return custom values
// author: @jimakker
// usage:
// if paramater "all" is passed will show all custom fields (for security concerns it's better if you don't use this unless you're debugging, and remember to remove/comment those lines once in production)
// else, will show the custom field named as the parameter (only one for now), returning null for inexistent value
// if no parameters are passed will return "null"
function json_feed_custom_vars($custom_vars)
{
if (get_query_var('custom')==''){
return 'null';
}
// Delete these following lines if you don't want to expose all your custom fields
else if(get_query_var('custom')=='all'){
return $custom_vars;
}
// This is OK, do not delete
else {
return $custom_vars[get_query_var('custom')];
}
}
add_action('do_feed_json', 'json_feed');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment