Transcript from the "Introducing WordPress REST API" Lesson
[00:00:19] We didn't look at the example using Axios, but there is a definitely a shift away from using jQuery for Ajax. And if you don't want to use native fetch, I'm actually a big fan of fetch, which is kind of the replacement for XML HTTP requests. But Axios is probably the library that most people are using now for their Ajaxy stuff, so we looked at all that and what we're gonna focus on now is the WordPress REST API.
[00:00:47] Now if you've used an API, it's a pretty simple thing, you basically go to a URL with this one in a browser and it gives you a bunch of data. So the question on how to use an API really lays down to two things. The first one is, what method are we gonna use to get the data, and that's going to be using Axios for Ajax call.
[00:01:31] So, [COUGH] let's come into our project files. And you wanna go into Section 4 on WordPress REST API, and you'll notice that these are not themes, they're not plug-ins, all it is is a simple Read Me file. So I'm just gonna open this up in my editor, so I can see it here, so I'm in 4.1, and all it is is a Read Me, because we're going to be accessing all this stuff in the browser.
[00:01:56] So this is just instructions on what to do and how we could do that so, you're gonna pull up your site.com/wp-json. And what we get here is the root of our API or the index of it, and if you've worked with APIs before this is actually fairly common for REST API that when you access the main root you get information about what's in the API itself.
[00:02:20] So once we do that, we should see that API and we'll look at that together and it should be pretty clear. Then we're going to go ahead and we're going to do something like, actually you know what? Yeah, I'll skip the slides, then we're going to, nope, I've got to go back, sorry.
[00:02:39] Let me show you this first. [COUGH]
>> Zac Gordon: These are all the possible roots, this is better to look at first, so if we go to site.com/wp-json. That's what we call the root of our API access point, so in any site now that has WordPress API enabled which is pretty much every current version of WordPress unless you go out of your way to disable it.
[00:03:07] You could access all their public data by the structure, so notice that we get posts, post revisions, categories, tags, pages, comments, taxonomies, medias, users types, statuses and settings. Now some of these have protected settings so the general public cannot just go in and look at all your usernames.
[00:03:26] And some things don't necessarily, [COUGH] behave how you might expect like If you have 100 users but only 1 of them has published a blog post or published a page on your site and the other people are just subscribers, you will only see 1 user when you go to users.
[00:03:43] So users are only gonna be someone who would show up on your author sections as an author on your site anyways. [COUGH] The other thing that we have access to are custom endpoints, so plug-ins are really big with this. If you're familiar with WooCommerce, which is the go-to e-commerce solution, along with easy digital downloads a little bit simpler, also very popular.
[00:04:06] You'll see something like woo/v something/ something else and then it will have a bunch of endpoints. And it's also possible to create your own custom endpoints and this is something that's important to mention. One of the examples we'll look at in a moment here is gonna be a custom endpoint that basically provides post data simplified.
[00:04:28] Because what you'll see when you go into /posts and you get a post data, it's gonna have a ton of data there. And there's actually several queries to the database that have to go on, it has to make one query to get the title and the content. It has to make another to necessarily get the name of the author, another one to the this piece of data.
[00:04:49] So, it is often pooling together more than one thing at a time, or just because it has to get so many different things it could be expensive. So when people talk about the benefits of the WordPress REST API and talk about speed as one of the largest things.
[00:05:28] Is that they do not vary often leverage the default end points because they could be quite heavy. What they do instead is figure out exactly what data they need, and then go and write some custom end points and just get that exact data. And then, everything is gonna be a little bit lighter, a little bit faster so, you'll have an example of how to do that.
[00:05:48] To really go deep you're going to need to know some PHP, but this will be a good start. The other thing about this is that, [COUGH] as we go to this URL to get all of our posts, if we add the ID you the end of it, then were going to get back a specific post.
[00:06:06] So same is true of pages, media, all of these different endpoints. If you put slash and then an ID number after it in some sought of formats it will be a little different for some of them, then you are going to get just that one post or just that one page.
[00:06:23] So again this will get us all our posts, this will get us one post of the Id of one, this will get us all our pages. Guess what this will do? Get us a page with an ID of one, right? This is not right.
>> Zac Gordon: I made the example a little too easy, okay, so you need that in it.
[00:06:54] And with this up I could talk about a few things in terms of the name spacing and how endpoints work. So you'll notice that we have, I'm sorry, let me open that up again. [COUGH]
>> Speaker 2: You just added -json?
>> Zac Gordon: Yeah, I added -json which is absolutely essential, it will not work if you try to go to /wp/v2.
[00:07:17] So wp-json will get us to our root index, wp will get us just WordPress endpoints. So like I said, if there are plugins installed like Jetpack and WooCommerce, more and more plugins now are starting to shift with their own custom endpoints. Instead of wp there you'd see custom or you would WooCommerce or you would see Jetpack or you would see God bless you or whatever it is, and then whatever version they're on.
[00:07:42] So they're not necessarily gonna be on version 2, right, they might be on version 1, they might be version 6, WordPress just happens to be on version 2. And once they go to version 3, the nice thing is, is that with this kinda syntax, if you wanna keep using version 2, it won't break anything.
[00:07:59] So a versioning was left off, let's say it was just /wp/pages/1, and then WordPress updated and change it's underlying architecture of how the API's returning data, it'll break everything. So that's why you want to have that versioning included and why it's there, and why the WP is there, is because you can also get other endpoints and other plugins and things like that.