Transcript from the "Challenge 6: Solution" Lesson
>> Zac: Let's go ahead and take a practice with this, shall we? Now, we could open up to 4.1 and we could see this will make a little bit more sense now. So first, it's telling you, go ahead and access the root of your URL, the root of your API, and see what's there and we'll look at that together.
[00:00:16] And then, go ahead and try to access posts. And pass a id to it. Now you might not have a post id one for some reason, it might've been deleted or something happened. So I'll show you how in the admin area to figure out exactly what id a post has with it.
[00:00:33] And then go to pages and you should see a list of pages and then we'll have a little bit of time to go to the other one so. Go to media, go to users, go to those other ones. Try creating a second user, that sort of thing, okay?
[00:00:45] So let's go ahead all and open up our sites and take a look at the WP JSON root. So if I go to mysite.com/wp-json.
>> Female: Is there a plugin to make it look all organized?
>> Zac: Yeah, there's a really nice one it is called thank you for that.
>> Male: Jsonview.
>> Zac: Jsonbeautifier is that the one.
>> Male: That's for wordcross really?
>> Zac: No, it's a chrome extension. There is one and then it wasn't there anymore. Okay, I think it's JSON view. I'm not sure which one I'm using, there's a few of them but different browsers have different ones and you definitely want to install one of these.
[00:01:34] That is an awesome question, thank you. If you ever wanted to get the name of your site, what's the URL of it. Again you kind of have to have the URL in order to get the URL so that's a little bit silly. But definitely remember how we passed in blog info name or blog info description through localized scripts before.
[00:01:51] Now if we need that data we could just hit our main JSON endpoint and convert this into an object and get .name and it will be the name of our site. The namespaces is another important thing so remember how I said there are certain plug-ins that you can install that will give you other namespaces.
[00:02:30] So that's it, we don't have authentication, anything in there, the biggest one we wanna look at is in routes, and so if i start toggling some of these down,
>> Zac: Notice that now we're starting to see the routes that I'd pulled up before and not only that we have things like pages but then you could pass an ID into it and there's our revisions one media
>> Zac: And there's some other information we'll see here, which are the methods. So it's a little bit outside of the scope to get into the depths of HTTP, as much as I would love to. But each route will let you know what you could do to that route, so If we're on media, we could get media objects and we could post media objects.
[00:03:26] If we come down to media with a specific id, we can get that specific media item. We could post new ones. We could also put and patch, which are versions of updating, so it's only gonna update that certain information you need and we could delete it, why do you think you can't delete all your media at once?
[00:03:48] Well, probably cuz you probably don't wanna do that, right? So that doesn't exist cuz it would wipe out your entire media library. They're gonna assume, hey if you really wanna delete something then you've gotta go ahead and tell us specifically what it is that you wanna delete. So this has a lot of different information about everything that we're looking for and this is our root or our index and we're done.
[00:04:09] So, now let's go to the next exercise wpv2posts1 so we put /wp/v2/posts/1.
>> Zac: Does everybody get a 1? If you don't have that, let me show you something.
>> Zac: So if I go into all my posts and I open up a,
>> Zac: A specific post up here in the browser, it'll say post is equal to.
[00:05:08] Mine is saying 1.28. If you ever need to get the ID of a post, you could get it that way. So go into the admin area, go to all your posts or all your pages, click on a specific one, get something that has valid id or if you go to all posts you'll see all of your posts and it will tell you the id right there, okay?
[00:05:31] But let's go back to post slash 1 or post 8, whatever gives you the data. So at this point, what we could do is explore what data is available to us. And every time you go to a different end point, it's gonna have different data available. But this is all of the basic post information you need.
[00:05:48] We have things like The ID, if you ever wanted to link directly to a url, if you put this ID in with question mark p equals, it will automatically redirect you to the right url structure for your site. We could get some date formats, the slug, the status, the link to it.
[00:06:10] Now this is important, notice how title is title and then rendered inside of it. So we can't just call post.title, we have to call post.title.rendered, and then we get what we want. And the reason is, is because if we authenticate and make the same request, we're gonna get more access to more data.
[00:06:30] So for example, we can get things like the raw data before it's filtered, or the four different paragraph tags or line breaks and put in and things like that. So there are for title, content, and excerpt, usually we want to use dot rendered, and we can never do just dot title on it's own.
[00:06:50] So it's a little weird thing that you'll get comfortable with. Notice then, we've got the author. There's no featured media. If there was, there'd be detail there and then a lot of metadata. So it just links to the author and all this other information. Watch what happens, though, when you put ?_embed and go ahead and type this in on your own.
[00:07:14] So what this does Is now if I scroll down,
>> Zac: Under links I have _embedded. So previously, all I had about the author was their ID, but now WordPress is going to go, sorry let me make this a little bit bigger,
>> Zac: Now WordPress is going to go when I say get the embedded content And it will get all of this information about the author, their description, the links to their Gravatar images, their name if they've changed it, the slug to them, all these other information, right?
[00:07:53] It also will get us replies so if there are comments it will pull back in. And then wp:term, so this will get us before we add categories. Categories and tags are part of a larger term in WordPress called taxonomies. And so a category could be considered a term.
[00:08:18] And so this will get us, not just the ID but the actual category name, right? So this is more helpful for us a lot of the time. And this is why we would use embed. Now, a lot of folks use embed and pretty regularly, but making all these extra requests to pull in all the author data, all the reply data, and all the term data, especially the more and more you have.
[00:08:40] Then that makes the request more expensive. So, again you really wanna thinking about,just because is it possible to do it and get more data, is it the smart thing to do?
>> Zac: Let's look one last example go to pages
>> Zac: And pages should have all your pages. I've only got one page, but it's got my content here.
[00:09:08] Notice that again title.rendered will give us the title and content.rendered will give us the content.
>> Zac: And the same thing happens where we have the author but no author information. But if we put ?_embed,
>> Zac: Then when we come down underneath the links then we've got all the author information again, no replies because it's not a comment.
[00:09:36] But we have those extra details, cool? So what we'll do is put like two minutes on the clock or something and try accessing some of,
>> Zac: These ones, okay? So I'll leave this up. We already did post pages. See if you can find specific ID's. If not, just put them up.
[00:10:06] And again, you might only have one uncategorized category. So then go and try adding some more, assigning it to a page, really see how things adapt and change and what format you'll get it back in