 No, this is a through hike of the WordPress API if you went down to the food truck You already got your warm-up hike in because that was definitely a hike down there, but the food was good. I really enjoyed it So is anybody here hiker or a backpacker has done any hiking backpack in their lives? so I like to Put little themes behind my talk make a little bit more fun things that I'm interested in as well so we're Gonna prepare you for your through hike of the WordPress REST API So through hike if you don't know is considered hiking a long trail from start to finish kind of in one go within like one season the summer season of hiking something like that or while a Section hike is when people will break up little sections of a hike of a long trail and do one at a time Maybe over a matter of years So Now we prepare for this through hike It's not easy, so we'll help you get through with this little primer we have here Now the US has a system of national scenic trails. It's the map of them They're all public lands free for all of us to access and use and enjoy they're managed by the National Park Service Volunteers take care of them through the whole system here. So I mean kind of like the open-source software of hiking and backpacking so Many of these trails are thousands of miles long the stretch carts cross large parts of the country So I came from way up here in the Upper Peninsula, Michigan Right by the shores of Lake Superior the North Country trails one of the ones that runs along You know right now near where I live. I've done a lot of hikes on that throughout Michigan, Wisconsin, Minnesota And when I was When I was thinking of ideas for a talk I thought about this and down here about 80 miles north of us is a southern terminus of the Appalachian Trail or the AT it's called so Starts about 80 miles north if you look it up on Google Maps at Springer Mountain And this is a spot where most through hikers in the AT will start So I figure it's a good place to start your journey on your through hike of the WordPress API so some of you may have Watch the movie or read the book a walk in the woods booked by Bill Bryson the movie stars Nick Nolte and Robert Redford I like the book a lot better than movie, but that's usually what happens and This can be a grueling journey and a pretty funny journey and Bill Bryson's experience The full trail stretches from here in Georgia to Mount Katahdin in Maine. It's about a total of 20 20 2200 miles So for most people this through hike takes about five to seven months to complete So I think the quickest ever done was like 47 days and change which is kind of crazy so Five to seven months. I hope you're all ready for a six-month-long presentation here if we're doing a three-hike to All right But there's some things you don't have to worry about like six months long You don't have to worry about bears on this through hike You're not gonna worry about hanging up your lunch up high to keep keep away bears or Being the soft half was the bear world Also another thing you don't have to worry about is how to squat next to a tree We have plenty of bathroom facilities here. You're good to go But if you're wondering about how to squat next to a tree don't worry I'm not going to demonstrate But are you I did a video on YouTube if you do want to find out so look it up If you're really interested we're not going over that so and thankfully For you guys, I was told I only have 30 minutes. He's over there making sure I don't go over so no six months so So there's several reasons you're gonna use the WordPress REST API like several reasons people Have for their through hikes. They're hiking backpacking trips Some people like do things for exercise some people have to have different goals in their lives They might want to go out and find themselves kind of like Reese Witherspoon's character in this book and movie But I don't know maybe you will but I don't probably not going to find yourself in an API maybe But we'll go through a few ways that you will use the WordPress API We'll go over some definitions of the terms being used in here And then we'll look through what WordPress has available in core and then how you too can extend that API to build things for yourself so I Figured to start it off if you don't know what an API is we'll start off with definition because We get a lot more confusing if you didn't have any idea of this and we start moving on from here so API stands for application programming interface and basic terms It's a way for two programs or websites other applications to talk to each other This is a definition. I believe from Wikipedia But most everything you use out there now has an API so you're Looking at things like Facebook Instagram zoom GitHub your weather Channel your weather apps Things like that project management soft management softwares they all have API's now so they can interact with each other So if any of you use github and say you use slack a lot of times with the integrations that work together where Pull requests from github will get pushed directly to slack so you're notified about them in your channels That's using the API with those Even things like if you use Strava for your Tracking your runs or tracking your bike rides They have an API as well and the up and also they use an API from other sources to play Provide the weather along with your runs and your rides and they integrate with say the Apple health app on your iPhone So you can have the data in both places at once So how is this used in WordPress? There's several ways you can use it one the big ones now is the block editor So everything out there now is being built with the block editor There's gonna be some good talks on this During the word camp here, so we're not going to go into everything the details of block editor, but It's not the way you build pages within WordPress I'll built now using JavaScript not all the PHP behind it like used before there's still PHP way in the back end of it But the front interface you interact with now is built with JavaScript And that uses the WordPress rest API to communicate From what you see in the screen to the PHP in the back end that puts all the stuff in the database Another one is headless WordPress So not just it's a buzzword out there. It's not just because it's the Halloween season That we're coming up head the headless horsemen here but If you were at the talk just before lunch when you guys are probably a lot more peppy before Eating and getting bogged down with all the food There was talking Headless WordPress go to WordPress TV after he had a lot of great information on it So I'm not gonna get in details on what it is, but basically It's decoupling WordPress back-end from your front-end views so you have You use the WordPress content management system still build stuff in your block editor or use the custom post types All the features you have when you're editing in WordPress, but you don't use the normal themes and Front-end display made in PHP and WordPress instead you can use it in things like Like maybe a static site generator one instances Gatsby These type of things will speed things up a lot because you're not processing the PHP in the background as well as Displaying the front-end stuff Also one-page applications, so you build things with React view angular a lot of these new technologies it gives you also gives you a lot of speed a lot of Performance improvements, plus you get to use in the cool new features build some nice interfaces along with With these tools along with your WordPress and you still have WordPress in the back end to manage all your content all your data The you know the beautiful system we all know and love there in WordPress one of the easiest platforms for that purpose And there are several host of providers the several frameworks that allow you to Build these things with your WordPress site using the WordPress API so Another option is if you were looking to build apps or make third-party integrations WordPress of course has their own mobile app that you can get on your phone to manage WordPress Manage your site or manager WordPress comm site. This uses Yes, the API to make those Connections you're not going into your logging actually into your WordPress site within the PHP You're using the API to make that connection to your site and bringing data back and forth You can also there's also many services or platforms out there that allow you to build your own Mobile app powered by WordPress using the same features of the WordPress API plus with third-party integrations just like I said that Strava hooks up with Apple Health or your Zoom or GitHub hooks up with your Slack You can make integrations with your own website to any of these services that have APIs as well or even say you had some legacy system within your company that you're working for and As long as the system has a way to connect out to the and make a request Outside of the organization into a different outside the organization but outside of itself You could build something say you're producing widgets on the line and you want to post to your Intranet that the hundredth widget was created you could build something theoretically like that That would use a WordPress API to create a post like that So The biggest thing when planning a hike like a topic is to be prepared. It's the model for both the girl scouts and the boy scouts So you can find tons of information on what to take with you on a hike on a backpack and trip online you can go to places like REI They'll walk you through your pack everything set up for you and and We're gonna look at the same thing kind of for what to pack in your WordPress API pack How to get ready some tools and some tips that can kind of guide you along this journey as you start to build things with API So a refresher and bringing up the API definition again. It's a way to communicate between two websites to applications To programs of some type communicate from one to the other Another term you're gonna hear in our acronym soup is rest. This is a rest API Here's the definition up here I'm gonna dig deep into this but basically It's kind of a set of constraints to specifically work on the web Allowing Well with the constraints It helps you make sure that things are similar when you're working with different APIs. So You're not worried that one thing's gonna be totally different than the other Next thing coming up is a route according to the WordPress developer handbook a route is The name you use to access endpoints. So think of this. I think of this more in our hiking analogy here as The section of the trail to your destination you build your route you find your route to get to your destination Follow down the path. So if you're looking right here our path is In WordPress version two of their API you're getting to posts. We're getting the post number one two three That's your path as you're hiking down The WordPress trail system here, I guess you'd say the route Next one up is an endpoint Guess kind of my analogy for the endpoint is maybe you're looking at your destination along the trail So maybe you're looking for a nice look up like this You know, there's some places you want to seal on the trail Maybe there's a waterfall on your hike You want to get to this act this position this place along your trail so Functions in computer terms and WordPress to him. It's a function you want to do to the API So for example, you want to retrieve a post you want to get one of your blog posts out of WordPress One of your custom post pages give that information out or maybe you want to update the post delete a post plenty of other things out there will go through a Couple of them that are available in a bit So where do you find these relative endpoints, how do you access the WordPress API? Well, it's going to be on your website. You're gonna take your domain Wp-json If you'd go just directly to this URL on your site, you will get a Request to the WordPress API giving you some information That one shows you basic information about your site and some information on some of the routes that are available on your site And if you want to get down the route you want after that You would add on the other parts of the route so WP V2 posts one two three like I talked about earlier You attach that to the end of your URL But how do you do this? Through these HTTP request methods These indicate your desired type of action so think of this like When you you have to say what you want to do once you get to your point you might say look at that waterfall, maybe So you get to this part in your route to the endpoint you want to get to and you set a get request in there To get the post that you want to get Post request would typically submit data a delete request would typically delete a resource delete some data There's other ones too. These are three of the major ones. You'll be seeing if you're using APIs Okay Little example, so this kind of running through whatever you showed here For example, you start with the URL I said your domain up there followed by WP JSON to get to your actual JSON IPA Or your actual IP API API not IPA. I think I'm thinking about the after-party And Then go to WordPress V2 post one two three that gets you down your trail on your hike through the API Your route on that is again WP V2 post one two three So one thing it says is the route doesn't include the WP JSON because that's just the base path to get to the API And so this specific route has three different endpoints One of them will trigger getting the post data one will trigger updating the post and another will treat Will trigger deleting the post depending on what type of request you send to this On this route here. So how does it look like when you get this data? It comes back in JSON again another Halloween reference here or Friday the 13th because that was yesterday Not quite as scary as JSON Varhese, but to some people maybe JSON can look a little scary But what it is is a standard format for data in JavaScript, so Call JavaScript object notation We'll take a look At what some of it looks like this is on the word camp central website. I think it's just basically hitting there slash WP dash JSON and As you'll see it has the curly braces which Indicate you're within the object you can have Key value pairs so the name of the site is word camp central URL central dot word camp dot org you have all that information laid out nicely for your Application the other application communicating with the site to take in Then you also have these ones right here these namespaces which aren't in a Name value pair there there in an array. That's just like a numerical array Zero one two three four like your normal arrays if you do work with wordpress or with PHP or with JavaScript But they're JavaScript arrays Just a note are with the square brackets And it's an object if it's with the curly brackets and the objects will typically have a Name to the element in it so on to some tools and tips for when you're working with the API Wordpress.org has the developer handbook There is plenty of information there on the API It lists through Every one of the endpoints built into wordpress core This maybe think of it as your map of the trail you want to know how to find something you consult your map So you look where you're going Developer handbook will have it all listed for you So you can plan out your journey Move on next to another tool that I would definitely recommend Taking is called postman This what this is is an API client app that is available on your computer it allows you to Put in your URL put in some parameters and then brings back that JSON for you easily readable Allowing you to actually see what data you can be working with very good for testing very good for learning what an API does even saves You can save queries on there. So if you want to get back to him and everything Maybe think of this as the GPS unit you're using on your trip here to make sure you're going the right direction Another option like this too is within your browser. So I told you JSON can be scary That's the scary up there That's what it looks like in his raw format, you know, we went back here. This looks kind of nice. I went back to the other This style right here looks pretty nice If you bring it up in most browsers right away, it's gonna look like that big jumbled mess on top That's why for there are browser extensions for most browsers Firefox I believe has it built in but there's one for Chrome JSON for matter. There's one for Safari that will make Your JSON pretty They'll prettify your JSON So you can actually read it and not have that mess up there You can actually see what's in the objects and the arrays that you're working with and The most important tip most important tip for I'm going to say for hiking backpacking and web development is Bring snacks You'll want to be hungry on the trip. There's this Tick-tock out here if you're on that going around I've seen that it's not about the hiking it's just about eating food in cooler places and I can't disagree with that either All right, we'll look on to some of the routes and W WordPress core Now there's an endpoint there routes for most anything you're gonna work with the stuff You're doing in the WordPress admin or maybe stuff you're building in plugins using php You can access pretty much all of it Through an API as well. Like I said look to the developer handbook for everything Because if I went through every single one of them and showed you every single one of them We might actually be here for six months So there's like I said, there's posts edit posts delete posts Pages comments nas menus users plugins On and on through all the parts of WordPress in the WordPress API Okay, and how do you work with the WordPress API? I told you The HTTP requests, but how do you make these requests? Well again, there's many ways to be able to do this because every language out there Pretty much every language out there will have their own way to make the HTTP request So there's ways in jQuery is ways to react view angular straight JavaScript You can access it from the command line curl is one of the main and oldest ways to make these type of requests Again if we went over all these we would be here for a much longer time as well, but there's a ways to work into it you Go ahead and pass in your URL passing parameters and try to get back and get back your information from your WordPress site the other term to think about is the schema of your data so What the schema is is your data return back in a consistent format? WordPress created a certain schema of how things are set out and what data is typically returned So with WordPress posts you can know what to expect So they'll have you know your normal things like your date your ID your do it your slug Status content title author all those things will be Available for a post and similar for other parts of data within WordPress And there's arguments. I talked a little bit about mentioned a little bit things about arguments here So say you're trying to get some posts, but you're not going to always want every single post from your site You're going to want to pick Post based on certain elements certain parameters So think that WordPress WP query if you're a PHP developer with WordPress You probably have made a query in WordPress using WP query similar to that you can pass in arguments To the API to get back different types of data. So similar there some different different wording to them but similar Information here, you can get how many per page? You can do a search with the author Excluder include things the order you can query by several different types of parameters And this is for posts There's different ones for users different ones for other data within WordPress and I'm showing our get request up here. So we want to get So we're going to WordPress version 2 of the API I just talked about this a little bit I mentioned this earlier, but after that WP JSON, they typically is a namespace Which is WP in this case WordPress is core stuff and then the version of the version 2 of their API So if you change things later, you can make a version 3 etc And then we're checking for posts and Then we're putting the parameters you can put this on the end of the URL in most cases or The methods of You know querying this sometimes they have other ways you can pass parameters along as well Like I said, I'm not going to go into every single way that you can access an API But you can pass different parameters along in this case. We're passing we want five Returned in a page of them. We want them all published by author with the ID number of two And they want them ordered alphabetically by their title Authentication so think of this one if you know about hiking or Backpacking sometimes you need to actually get a Permit for a hike. This is kind of the permitting process. So say at this hike right here. This is the wave It's a really cool place in Arizona They only allow so many people in there per year. You have to go into a lottery for it But it's I mean I'd love to go here. It's super cool. It's like this whole wavy Formation in the desert down there But like I said, they only allow X number of people per day X number of people per year Because they want to protect this place. Otherwise if we get overrun with two risks things will get ruined You know You want to protect the stuff on your website in the same way Sometimes you have similar you have sensitive data out there. You don't want to be able to Anybody just to access that but you want a certain application or a certain set of people to be able to access that or If you want to make the Creating posts you're not going to want just anybody out there to create a post you're going to need to authenticate To get that post created So this provides it access the authentication and you can pass along authentication parameters in your request as well and there's Several different ways of doing authentication with an API Again, I'm not going to go into all this or unless we want our six-month talk But a few of the different ways that are common are used WordPress itself uses some cookie authentication in it. So if you're working like directly In your WordPress site, but access in the back end through the API that would be available in some cases They have a way now of doing some basic authentication with application passwords that you can use They're often used one is this JWT Java web token Authentication plug-in that's able to use that for your authentication and there's also an OAuth plug-in And there's other ways out there as well But these are a couple things to look into as you continue on in the journey Plugins to now. It's not just in WordPress core There are API routes in many of the major plugins probably most of them out there have added them now So look at WooCommerce, ACF, Gravity Forms, whatever we have out there Most of all have an API now So you can access data through those plugins or create data using those plugins Through the WordPress API in a similar fashion show down here I did put down like Example if you're going into WooCommerce, so it uses instead of WP it uses WC as the namespace for your API request here and or for your route and then you're You know retrieving your orders from the WooCommerce site And now if you want to blaze your own trail and extend the WordPress API Obviously the plugins added it. It means that WordPress like with most things makes it extendable It makes it allows you to build your own API endpoints You can modify things on existing endpoints add data to them as well You make custom post types you can make sure those are available In the WordPress API as well because you don't want just the posts and pages All those other cool stuff you're building on your WordPress website You're gonna want to be able to use in your applications your custom builds that you put out there You can even add endpoints of Any custom data you want that they don't have to follow the same schema as The WordPress post you you can build your own schema if you're building a fully custom application And push out data in different formats if you'd like Here's an example of adding custom post type support for the API You're doing your normal register post type and as you create that you want to make sure this Show when rest argument is on here as true. I believe that does default come across as false So if you want it to appear within your API you definitely want that to be true So you can then this is adding your own custom endpoints a little example here You can build your own endpoints like I said to return any data you want It doesn't have to be just like a custom post type like the posts are you can build your own type of data coming from Whatever stuff you build within WordPress So with this one looking at it you're gonna add it into the rest API a knit action and Use the register rest route function so you Then create your namespace in the first argument In this case my plug-in with the version attached to it version one of it You want to have that namespace so it doesn't get mumbled and jumbled with any other types of APIs added to your site and You can identify it from everything else and then You continue on your path from that in this case This is one. I just took from the WordPress developer handbook. It's author and then it uses a regex to I Don't actually regex but something to Actually find the ID of that author. So this is searching for an author And it will use the get method we talked about the HTTP request methods earlier And the callback is the function then that you want it to run So when you put this request in there, it runs that function. It'll then you'll then want to return the data Within that function that you want out on your API So this is the end of my talk, but the beginning of your journey on the through hike of the WordPress API So we went through what an API is some tools and tips We look through some of the things core has to offer and showed you how to blaze your own trail So the Appalachian trail like I said starts down here in Georgia for and for through hikers It typically ends at the top of Mount Katahdin in Maine about 2,200 miles away 2,280 miles from here. I guess it's a miles away goes through 14 states and Takes about six months five to seven months so enjoy your through hike of the WordPress API and You find more information about me and my website and we have a lot on there But you want to contact me and I do have slides on there and just let me know if you have any questions Open for questions now I wish I had a good answer. I mean I've used that JWT off plug-in before and I've done some basic got stuff, but I don't really have I Don't know. I don't really had the best answer for it. So and if for he asked what the best way to persistently authenticate was but You know as anybody else has any suggestions they can speak up as well Any other questions? Yeah, he asked If by default when you install WordPress is the API Active and there was a way is there a way to turn it off and the answer is yes It is active and yes, there is a way to turn it off in most cases. You probably don't want to turn it off like I said the block editor which is Big part of core now does use a WordPress API. So if you did that the block editor would immediately stop working But there is a way to do it. You can put a snippet within your functions dot PHP or within a plug-in that will disable it And it is on by default Else all right. Well, thank you and enjoy your hike