 as an alternative to XML. I pulled this off of Wikipedia. That's what it said. That's what I'm going to take it as. It's easy for humans to read and write. And it's easy for machines to parse and generate. So that in and of itself isn't a very common crossroads for humans to be able to understand something and a computer to be able to understand something. Where those two things intersect, not very frequently. It doesn't happen frequently. What is the WordPress REST API? So this is a JSON output. We're going to kind of look at this in a real life example here in a second. But WordPress can use its REST API to send data out using JSON format. And like we mentioned, if it's easy for a computer or the internet or a browser or another web application to use this, JSON kind of becomes a de facto best practice, wouldn't you say? So a little bit of a history lesson, a little bit of defining the landscape. So WordPress already has a feed functionality. And you can push things out using RSS. So what is RSS? It's a rich site summary or originally RDF site summary. You may have seen an RSS feed before in your life. We'll actually look at what that looks like here in a second. But RSS is based on XML as a markup language or extensible markup language. XML is subsequently based on SGML, or standard generalized markup language. This was invented back in the 70s. And then it kind of spawned XML. Another spawn of SGML is something that some of you may have heard of called HTML. It's a markup language. The one thing that XML is different from HTML, I'm sorry, is that it actually has to be parsed. You have to have some sort of technology that takes XML, registers it, and actually makes it usable. It's not automatically out of the gate usable. HTML kind of is. So that's kind of one drawback. That being said, it is kind of a legacy way of syndication and sending data out from one application to another. It's there. It's useful. WordPress has historically used XML quite a bit and RSS feeds. But before we get to why we would use JSON, let's kind of look at that code just a little bit. So this is an RSS feed. If you're looking at this and saying, wow, that looks like HTML, you're not totally wrong. So you'll see that there are divs where it starts off with a channel, and it kind of comes down to a title, tag, items, and then paragraph tags and strong tags there. Looks a lot like HTML. Kind of begs the question, why would you use this instead of HTML in the first place? And we'll get to kind of what it looks like to have to deal with this format instead of JSON in just a little bit. So going to JSON. JSON is parsed and ready to use JavaScript objects. JavaScript is super easy, super modern. Most modern developers are using JavaScript anyway. They're using JavaScript framework. Some of you may have already had experience using this. But JSON is really simple. It's very fast, and we'll break down a few of the pros of it in a second. But it's lightweight. It's a lightweight data interchange format. It's a simple array structure. So you'll have one object. You'll have something inside of that object. You'll have something inside of that object. It's really simple to follow. So like, whizbang, boom, you understand it. A computer can understand it. It can ingest it and spit it out and do something with that data. It's easily parsed by a browser. It's also basically universal because any kind of coding language can just import JSON and do something with it. If you want to use XML, sometimes you have to install an extra package just to parse out the XML format, which if that's not a problem for you, fine. We're going to discover some problems with it in a second. So what does JSON look like? It kind of looks like this. It definitely looks different. It looks nothing like HTML. But you have a key pair value that's broken down into an array. So it's really simple to say the value of slug is alert and the value of status is publish. So let the syndication begin. How do we use JSON to syndicate? The goal here is to take WordPress content out of one site. And we're going to use WordPress JSON to spit this out to another WordPress website or infinity websites or just an unknown amount of other destinations. Live demo, so really quick. This is an example. And again, this conversation is more to conceptualize the possibility. This is not the end all be all. This probably might not even be the best practice. But this is a definite use case of what JSON could be used for. This is one implementation to show off how simple it would be to use WordPress to feed data to another WordPress website in a multitude of ways. And then we'll also talk about how we scale that. So say this is a school district alert portal. Children go missing occasionally, or they're lost, or their parents just can't find them. Whatever. School district alert portal. Say there's a missing child out there. Whoever manages this, district security, district police, the district office, someone there who just needs to get the word out, can log into this website, and they can create an alert. So let's just say that this is an alert. There's a missing child, and his name is Chunk. He's 11 years old. He's 4'1". His last scene searching for the treasure of one-eyed Willie near Astoria, Oregon. And of course, we definitely need to have a picture of what this kid looks like. If you haven't seen him, how dare you? Anyway, so this is a nice easy human readable format for this data. And that's what WordPress is really great at. But every WordPress website, you kind of see the domain here, alertsite.wpngen.com. If we go to JSON slash WP slash WPV2 posts, we have the JSON output. Now this is not very pretty looking, but again, if we just kind of go one more tab over, this is that same data. It's just made pretty. And we have these key value pairs. We can also see that his name is Chunk here, the same data last scene searching for the treasure of. And it kind of parses that out. JSON knows what to do with that pound sign 8-2-2-0. But that data is there, right? Cool. This data is easy to send from one application to another application, from WordPress to something that isn't WordPress. Or for our example today, we're going to talk about from WordPress to WordPress. So with this, how can we get this to be ingested by another WordPress instance? Using WordPress native features like PHP. It's very common to WordPress. So in that example, first we'll look at what you have to do to parse RSS feeds. This is a piece of code that we're looking at to parse out an RSS feed. We have a URL here that would suck in this feed. And we're going to use a curl option to do that. Once we come down here to the loop, we're going to start grabbing things out of that feed. But remember, XML kind of looks like HTML. And it's got those big chunky tags that say like paragraph or header. And we have to compensate for that with our code here. We're going to echo out part of that file. We're going to say, hey, here's like an H2 or a header2 tag. This is the start tag. This is the end tag. And say, just grab whatever stuff is between there. And then call that item title. Does that seem cumbersome? Does that kind of seem confusing? It's kind of like saying, if you wanted to get a book off the shelf instructing someone, hey, look on that shelf. There are these things called bookends. Now, between those bookends, there is these wooden things, wooden paper ink things. Grab that wooden paper ink thing and then call that book. That's what this is having to do. We have to compensate for this format to get this out into an RSS feed. Now, looking at something very similar, how can we do this using JSON and PHP? A plugin. So this is a six line code plugin. Of course, all of this stuff here is just mandatory for any plugin like a description. So let's just kind of like remove that for a second. This is the code that we're looking at. So for example, right now, we're just going to create a short code. Let's start from the bottom. Now we're here. So we're going to create a short code and we're going to call this short code alert. That short code, when input into a WordPress post, executes this alert function. What's this alert function do? We're going to grab some attributes. Now what are these attributes? We're going to go ahead and grab alertsite.wpng.com in this post URL, all that JSON that we talked about that had the data about chunk being missing. We're going to assign that to result. Then we're going to use PHP JSON decode. This grabs the result. Now the body. This is something that's just part of that JSON array. Anything that's in that body, we're going to start trimming down exactly what we want out of that. All of it is going to be called posts. It's going to zero index. So it's going to start counting from the number zero. So we're going to say zero, the first instance that you find, grab that. Because we want the most up to date missing child. And hopefully, we find the children eventually and they get, you know, we can circulate through this list. And then we're going to grab that first post and call it post. And then what we're going to do is just say, hey, return content rendered. Content rendered in this scenario are two elements that are in this array. So we start with body, then we grab with that post and then we grab content and content rendered. So it's like, whiz bang boom. We're just sifted down using a couple lines of code. What does this look like on an implementation perspective? We install this plugin, we activate it. We're going to go back to our WordPress website and do something like this, alert. What is cool about this? Using a short code automatically lets the words from that JSON output fall into your WordPress styles. So your fonts, your colors, your backgrounds, whatever. That is already going to fall in line with your stylistic changes. And from a user perspective, you don't have to be a developer to know alert. It's pretty simple. Once we save, we can go to our school website, which here is at a different URL, wpdistrict.wfinish.com. We have a district alert page, but it shows up here. All that data, even the photo showing up on site too. So great success. Pretty cool. Anyway, what can you do? Now we can say this plugin needs to be installed. It's a must use plugin on every single site in our school district, every high school, every middle school, every elementary school, every single portal that a teacher might need to log into if some school districts are using WordPress to do that. They can now have this really easy to use portal to make this alert district wide. So a couple hundred websites now have this plugin. Couple thousands of people are now searching for this child. Some of you may have had a red flag go off in your head to say, what if 500 every single school in the state had this alert on it and they're all making requests to my origin website. My origin server is gonna blow up with thousands of PHP requests. So now this is what we talk about exponential publishing. You have to think about exponential scalability. How can you scale my origin server to compensate for this kind of load? There's the really old fashioned method of we could throw hardware at it. Sure, if you were gonna set this up on like a auto scaling technology, it could totally do that. But how can we be more intelligent with our scalability factor? So this is the code that we use to make this plugin. Anytime someone goes to this page, it's going to execute this PHP function and it's gonna make a PHP call from site two back to site one to grab this data. That happens across a couple hundred websites. It's gonna kind of suck for site number one. So one way we can do this is use WordPress Transients. Who's familiar with WordPress Transients? Cool. So when something is transient, it comes and it goes, yeah? We're gonna use WordPress to grab this data and it's gonna hold it. It's gonna hold it for a little bit of time. It's gonna hold it in its database, but instead of just keeping an ongoing copy of every single missing child that's ever been missing in the history of humanity, that would begin to bloat in my database, yeah? And that kind of starts taking up more and more resources when we don't want it to. So hopefully this child no longer is missing, they're found, and then the next child that gets lost on his way home, whatever, we're able to remove that data, grab the new data, and we have data coming in and out, data coming in and out. But what this does is since we're storing it, we can store it for a period of time. What does this look like? Same plugin, just a little different. Tweaking the code, just a tad. We still have a short code, we still have attributes, but now what we're gonna do is assign this post to a get transient. What this allows us to do is set some technology to say if false posts, like if the post doesn't exist, then go ahead and run out and go grab that JSON data, and then it's the same business as usual. We're gonna grab the body, we're gonna grab the very first post that shows up, we're gonna grab the content rendered out of that very first post, but what we're doing here is a little different. Right here, we're going to say the value of that alert post, store that for 12 hours. That way in the next 12 hours, we can go get that response, drop the old data, get the new data, and spit that back out. Now, this one individual website and this one plugin is not making calls every single time that page loads. Say every teacher loads that page, 200 teachers load that page, 200 times 500 schools, times however many students have this. It's really gonna suck. So, if we can actually store that data out on those individual websites, now we're actually using WordPress to be a CDN for another WordPress website, or it's another caching mechanism for a WordPress website. We're using WordPress to feed more WordPress. This is one way to do it. So, one way is we can set a transient again to store that data temporarily on the requesting application, the requesting WordPress instance to store that data. Another option would be to cache the response locally on my application server. I could use varnish, store that data, that way every time it comes back, my server doesn't have to generate that response again. It just kind of already has it saved and it can send it out easily. This might accrue some kind of bandwidth on your origin server, but it's not gonna blow up, maybe. It's possible still. Talking about if we wanted to have an application search for a child nationwide, if we wanted to have a nationwide child alert system, that's not just gonna be limited to WordPress. That's not just gonna be limited to other WordPress schools that are using WordPress. This is news, this is media. This is going to have millions and millions of potential hits to gather this data to look for a missing person. One really cool way that I've seen people do this is they actually take that JSON response and they don't store it on their WordPress website anymore. They actually update a JSON file and using Amazon S3. Who's heard of Amazon S3? Few of you have. So it's simple storage service. That is infinitely scaling by default. Penny's on the dollar. If I have a JSON file up there that I have thousands of websites calling out to millions of times, it might cost me five bucks. It might cost me 10 bucks to manage that bandwidth. But this allows us to get a message out there to thousands of people, thousands of other applications. And now we're talking about exponential publishing and scaling for that kind of publishing. Kind of cool. Fun stuff. I know I have a lot of enthusiasm and I'm feeling it from you guys. It's cool. So yeah, this is one thing I was like, oh yeah, I guess you could do that. So now like my origin server is kind of like zero bandwidth and I'm just sending a ton of stuff out using JSON. So far, we've talked about what is JSON? It's really cool. How can we actually use this in a real life example using PHP? Again, this is just an example. There are a lot of things you could build out on top of this. You could build into this authentication so that only some people can use the JSON feed and not everyone. I'll also say that a few of you may have heard of something called NPR. So NPR actually uses a regularized feed to send out to other websites. If you've used WordPress and ever installed like the NPR WordPress widget, you can use that. It also goes out to their affiliate radio station websites. Goes out to iPhone apps, Android apps. And backing all of this, there's nothing to say that you couldn't do the exact same thing using JSON. Because where's this data gonna go downstream? Could go anywhere. Not everything likes XML, but everything likes JSON. It's totally fine to use. So, exponential syndication. Plot twist. We're gonna talk about aggregation. So how can we actually get feed from thousands of sites or a random number of sites back into our one single website? I've seen this with customers where they have a lot of data and they want to have this come in from a lot of different feeds. Feeding into one new syndication. I've also seen this where I work at WP Engine. So I was throwing this idea out to one of our marketing developers. And he was like, yeah, we use that. We use it right here where if you go to wpengine.com slash newsroom slash thought leadership, these posts are actually aggregated using the REST API. If you're not familiar with WP Engine, we do a lot with WordPress. We have several different digital publications using TorqMag, Velocitize, and the WP Engine blog. There's a lot of things that we publish out over a lot of different mediums. We have guest writers, we have internal writers, and all of this is actually aggregated in this page using the REST API. And so I asked our developer, his name is Ryan Hoover. He's really big in the WordPress community down in South Texas. And I was like, can I see the code? And he was like, yeah. And I was like, can I show that code? And he was like, some of it. So I said, okay, I'll take what I can get. So again, this is a snippet. There's actually like a lot of lines of code here. But looking at this, we're actually assigning a variable to say, hey, go grab this post from TorqMag. Go grab the API URL from Velocitize and do the same thing for wpengine.com. And then we're assigning those to TorqPost, Velocitize post, WP Engine post. And then we're gonna combine all of these posts in an array merge using PHP. And that's what you see the product of on that one page where we have all of these posts from three different locations nicely being displayed in one location so that we can bring data to one centralized location and still have an easy user interface for it. There's nothing to say that you couldn't do this exact same thing with like a thousand websites. Scaling that destination server might be kind of heavy. But again, there's nothing to say that you couldn't use JSON to aggregate or syndicate. Trying to do this with XML or RSS, pretty difficult or just kind of weird. JSON is more modern. So, wrap up. I got so excited I skipped this slide. JSON is modern. We can use it to syndicate. We can use it to aggregate. The WordPress REST API friggin' rocks. It's really cool and it's really modern and pushing WordPress into modern methodologies to communicate between applications to drive higher engagement to audiences or from audiences and making it easier to get the word out or to get the word in. And that's all I got guys. Thank you for listening to me rant about some cool stuff that I've seen customers do. Q and A, questions. I know we're kind of short on time. I'm gonna talk you bunch. Go ahead. Pretty like some example. You showed the public the control of that. Good question. So the question is how can we build security around what we just talked about? Yes, there's definitely ways you can do this. So the REST API, you can have a handshake using like an authentication method to access this feed. So if I wanted to grant access out to that, you could build an external application grants this out or just the source and the destination would have to communicate with that REST API authentication. And that's mostly in general just for accessing the JSON feed URL at all. Another way you could do this is using an HT access rule where if you have the IP, if you have a few destinations and you have the IP address for those destinations, you could just whitelist those and blacklist everything else. That may not be the most secure way to do it, but it is possible. OAuth would be far more secure. Granting access to a specific post. Not sure how to do that, but there's a lot of PHP geniuses in this room, I'm sure. They could probably talk to you about how to do that. But yeah, this is definitely something, this is more just conceptual. Can you get the word out? Can you build out more functionalities to it? Absolutely. Question in the back. So right now the question is XML feeds kind of the standard way to syndicate? Yeah, that's kind of the standard way to do it. Most plugins that I looked up, there is like an RSS syndication plugin out there. And it gives you a nice UI to where you can kind of select what you want to show and where you want to show it and how you want to show it. It's very heavily built. It is very robust, I should say. Moving into building your own application. Moving into using JSON to not only speak to WordPress, but possibly to other technologies like a JavaScript framework. We kind of mentioned like an iPhone, Android app where I want to be able to create once and publish everywhere or cope NPR coined that phrase. JSON is kind of a, it's a lot easier to do that in the long run. But yeah, right now XML is kind of the incumbent. It's what everyone uses. This is more to kind of promote JSON as, I can do the same thing and in some ways it can do it better or it's just another option that fits developers specific needs. If you like XML, cool. Yeah. Any other questions? Yep. Can we post the slide deck? Yes, I can definitely do that. We'll have to find out where, if there's a WordCamp location or you can just kind of syndicate that. Then we can do that. But yeah, totally. Putting in custom fields, like data that's already been put into a custom field and sending that out. Yeah, so the cool thing about that is like if you have a custom post type, that kind of where you're at? No. Yeah, so the JSON feed does withhold metadata. If it's in the JSON feed, you can easily parse it out. I haven't built anything that uses that specifically, but I don't see any reason why it wouldn't be in the JSON output for you to manipulate and send and use wherever you want. We got one last question. Yeah, so good question. So that was taken out of a very lengthy doc. Yeah, it was legit just a post. We were actually doing some really cool things there where we actually bubble up a level of importance on a scale of like one to 50, where does this article rank as a means of like something that we really wanna promote or something that's like so-so, and it'll bubble it up to the top based on its gravity or our marketing gravity, however we wanna do that. We gauge that by what we see from Google Analytics, what's hot, what's not, and then we implement that here to aggregate and then sift and sort. So yeah. But I'm being told that we are out of time. So if you wanna find me later on, I'll be around at the WP Engine booth and I do like to narrow it out about this stuff or develop a workflows and whatnot. But thank you guys so much for coming and have an awesome WordCamp, Boston.