JavaScript for WordPress JavaScript for WordPress

Introducing How To Access API Outside of WordPress

Check out a free preview of the full JavaScript for WordPress course:
The "Introducing How To Access API Outside of WordPress" Lesson is part of the full, JavaScript for WordPress course featured in this preview video. Here's what you'd learn in this lesson:

Zac discusses the reasoning and benefits for accessing REST API outside of WordPress including using WordPress in a headless content management system (CMS).

Get Unlimited Access Now

Transcript from the "Introducing How To Access API Outside of WordPress" Lesson

[00:00:00]
>> Zac Gordon: Now we're gonna do basically the same thing we did before, we're gonna create decoupled though. So we lose some things when we move outside of WordPress.
>> Zac Gordon: So some of the benefits of being inside of WordPress are, we know what the URL of our site is, right?

[00:00:18] We could get any data and pass it through localized scripts very easily, and we can also do things like authentication very easily. So, the fact that all we had to do was pass in a nuance and everything works, or with the backbone client. You didn't do any authentication.

[00:00:35] Notice, when you saved, you weren't passing any crazy things, you weren't checking cookies, you weren't seeing if they were logged in or had permissions, right? It was all doing it for you. When we stepped away from WordPress and built it on our own, we've got to do all that on our own.

[00:00:50] But there are some advantages. One advantage is WordPress runs in a Linux environment, it runs with PHP and it runs with MySQL. You can't run Node well in that environment, it's not built for it. And you can't run WordPress well in a Node environment. So if you wanted to do server side rendering like pull the API request in and serve it directly to the page.

[00:01:12] And this is approach more people are taking because it's gonna get there right away and you don't have to worry about the fallback, that stuff is already loaded there, right? If you want to leverage server side coding, the WordPress.com site, the Calypso, it's not running in a normal Linux WordPress hosting environment, because it's doing special stuff behind the scenes.

[00:01:34] So that's something to keep in mind. And without any real good reason, I just think it is kind of fun and cool and sometimes that's enough reason to explore something until you realize that it's not the best approach. So, here's what we're gonna do outside of WordPress. Guess what, it's the exact same thing until we get here.

[00:01:54] So, figuring out all the URL requests, it's very easy. And when we talk about modular code, you'll find that most of the code that we wrote in WordPress, we can pull out of WordPress and use decoupled. And likewise, if you build some sort of app decoupled, you could use those React components, you could use those view elements, you could use that vanilla JavaScript, pull it right into WordPress and it should work.

[00:02:17] And then we'll just use this for the rest. So what I'll do is I'll show you the demo. And then we'll work through the first part together again. And then I'll give you some time to try to get to the rest and then we'll go back and talk about some bit and pieces of it.

[00:02:31] Particularly, I want to stop before we get to authentication and do that one together because it is leveraging some tools you may or may not be familiar with. But let me just show you the setup for these.