I’m not dead yet . . . Google Scripts to Check on a Bunch of WP Sites

John Stewart is going to write up something more systematic and structured as he’s taken these rough ideas up a notch but I figured I’d throw this functional Google Script code in here before I lost it. I believe I got up to five positive statements on blogging more in-process stuff so I’m taking that as an overwhelming mandate.

These Google Script functions are meant to loop through a set of URLs in a Google Sheet pulled as an array to see if the site’s still in use. The first two take a look at the WP REST API endpoints for posts and pages. That way if the person only writes pages you won’t be tricked. I’d probably write them all to the sheet because I’m paranoid.

The third (aka the hassle as I hadn’t ever messed with XML using javascript) looks at the RSS/XML feed in case the site is not updated enough to have a functional API endpoint or if it’s broken for some reason. This won’t help you out if they’re just writing pages but there’s only so much a person can do.

John made a more robust structure with error checking, the piece where it writes to the spreadsheet etc. and I’ll loop in his post once it’s up.

function checkPosts(url) {
  var api = '/wp-json/wp/v2/posts/';
  var response = UrlFetchApp.fetch(url+api);
  var json = JSON.parse(response.getContentText()); 
  var mostRecentPost = json[0].modified;//note last modified rather than pub date just in case . . . 
  Logger.log(mostRecentPost);
  return mostRecentPost;
}
function checkPages(url) {
  var api = '/wp-json/wp/v2/pages/';
  var response = UrlFetchApp.fetch(url+api);
  var json = JSON.parse(response.getContentText()); 
  var mostRecentPage = json[0].modified;
  Logger.log(mostRecentPage);
  return mostRecentPage;
}
function checkFeed(url) {
 var feed = '/feed/';  
  var xml = UrlFetchApp.fetch(url+feed).getContentText();
  var document = XmlService.parse(xml);
   var response = document.getRootElement();
  var channel = response.getChild('channel');
  var build = channel.getChild('lastBuildDate').getValue();
 return build;
  
}

Comments on this post

No comments.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL