Though many of the concepts in this course are still relevant, overall this course does not reflect our current course standards.
Transcript from the "Customizing API Endpoints" Lesson
[00:00:00]
>> Zac Gordon: Okay, so the next thing I wanna do is make sure that you copy the simple post's endpoint plugin. Make sure you put that in the plugins folder. And then come into your plugins area.
>> Zac Gordon: We'll make sure all our old stuff is deactivated. And then make sure to activate this simple posts endpoint.
[00:00:27] And what this does is, then on your site at .com/simple/v1/posts, you could see some even more trimmed down data about posts. So let's go ahead and copy this from WP JSON, all the way in the posts and put this in our URL.
>> Zac Gordon: So notice now, I'm getting all my posts still but look at the simplicity of this.
[00:00:58] Remember before, we had title.rendered and we had the GUD and we had all the date modifications and all these other stuff. Now what we have is the simple ID, URLs, slug, title, excerpt, content. Boom, done, right? So obviously this is going to be faster for the query to build and to get out to your site and that's gonna be faster to process.
[00:01:20] So this again, my recommendation for trying to slim stuff down a little bit and let's just take a quick moment and look at the code here behind it. So go ahead and open up that folder, jsforwpsimplepostsendpoint, and then open that main plugin file.
>> Zac Gordon: Again, this is PHP, and my expectation's not necessarily that you'll walk away knowing how to do this.
[00:01:47] There is pretty good documentation on the site that I showed you before. But the different pieces should make sense. So first of all, I'm registering my route and I'm telling them what it's gonna be, so it's gonna be /simple/V1 and then I'm only gonna have posts available. You could only get posts, so I'm not gonna let anybody save or delete posts with my code, and then if somebody hits this, what should they do.
[00:02:12] Well, that's gonna load this call back function which will tell it, tell WordPress what to do. So, coming down into that,
>> Zac Gordon: First we set up some arguments for this get posts function. So again, this is kind of PHP stuff but this is one of the simple ways in WordPress to get a bunch of post.
[00:02:36] So here I'm saying -1 means get me all of the posts. So I had set this to 3, it would have limited it and there's a bunch of arguments you could search for get_posts if you wanna do more on this. It's documented pretty well. If there aren't any posts don't do anything.
[00:02:51] But then if there are posts, so if I get posts back, then for each post, go ahead and create a new simple_post that only has this specific data, right? So I'm getting my post here, this is a foreach loop. And then I'm saying set ID, equal to ID set URLs equal to the unique ID.
[00:03:13] Set the slug, set the title, set the excerpt. This is going to apply any HTML to it so that you get access to that HTML otherwise it might come out unformatted which you don't want. And then push those all to this one array and feedback that array. So this is enough if you needed a custom media one, if you needed a custom comment or anything that you wanted to trim down.
[00:03:38] You should be able to tweak this and get to where you need to go. But if we come into the browser now and go to simple v1, we see exactly that, right? If I were to comment out some of these, and maybe I only wanted titles back, then it would trim it down even more.
[00:03:54] But notice that once we make this request and get our posts, it doesn't really matter what data we add. It's not really gonna slow it down too much. It's how many more other requests we need to then go make on top of that. So if I wanted to pull in the author name, that's a whole another request on top.
[00:04:11] So again, just stuff to think about and really that is all we need to know about practicing and getting comfortable with the WordPress Rest API. So I'll pause here at this point just to see if there are any questions about anything that we’ve covered up to this point in terms of endpoints, arguments, or custom endpoints.