Tweaking the WP Base JSON Data

Will Wonka asking you to tell him again what WordPress can't do.

We had a list of rampages sites in a Google Spreadsheet and wanted to know when they were created.

I started to look that up but only managed to do it twice before I gave up and went in search of another way.

In this case it took two little bits of code.

This first piece is active on our generic site-wide plugin. It adds the blog’s creation date, last updated, and post count to the base JSON data. That’ll be handy in the future if we want to checkup on sites with only one query rather than multiple queries.

/*---------------------------------JSON MOD FOR ADDITIONAL SITE INFO----------------------------------*/

function extraJsonData($response){
    $blog_id = get_current_blog_id();
    $blog_details = get_blog_details($blog_id);
    $data = $response->data;
    $data['created'] =$blog_details->registered;
    $data['last_updated'] =$blog_details->last_updated;
    $data['post_count'] =$blog_details->post_count;    
    $response->set_data($data);
    return $response;
}

add_filter('rest_index', 'extraJsonData');

This second piece is a Google Script that makes a function that I can call in the sheet by typing =getCreationDate(“http://someurl.com/”)

function getCreationDate(input) {
  var url = input;
  var api = 'wp-json';
  var response = UrlFetchApp.fetch(url+api);
  var json = JSON.parse(response.getContentText()); 
  var birth = json.created;
  return birth;
}

The two together answer my immediate problem but the JSON modifications have some long-term value for us and might be useful to someone else.