Though many of the concepts in this course are still relevant, overall this course does not reflect our current course standards.
Transcript from the "Enqueueing Script" Lesson
[00:00:22] So that's great. Then, we're going to kind of hack or hijack something that was not intended for what we're using it for. But wp_localized_script(); because WordPress runs in so many different languages and it's really big on being translatable, easily translatable, and easy to run in other languages and easy for other people to translate.
[00:01:21] So this is what enqueue_script( looks like. It takes a few different parameters and I have them broken down, line by line. So the first parameter that we have is a unique-handle-name, and we can name this whatever we want. Commonly, it'll be something like theme-js or mythemeness or like view-js or for loading view or something like that.
[00:01:43] So this first one is something that you wanna be able to, at a glance, look at and tell what it is. And then, in other places, if you ever wanna say like, hey, pass this extra data into this file, you'll call it by that name. The next parameter is the path to the actual file itself.
[00:01:59] And so there are a few different ways to dynamically get a file. So in WordPress, if you have a theme, all of your theme files are gonna be at like, yoursite.com/wp-content/theme/nameofyourtheme/asset/js/file.js, right? But you do not wanna hard code that whole thing because what happens if your theme name changed?
[00:02:21] What happens if they installed an earlier site like all these other things? So get_stylesheet_directory_uri() is going to give us a shortcut to get into that folder, and then be at the root of our theme folder and from there, we could say, hey, go into our assets js file and grab that file.
[00:02:59] So what this handles because in WordPress, we're not gonna be able to hard code anything, it could setup dependencies this way. So let's imagine I had two scripts, I had my helper script and I had my theme script. If this was my theme script where it says, dependency handles, I can say, handles-js, and it will automatically make sure that my handles one comes before my theme one, and we'll use this quite a lot.
[00:03:53] But if you change the version to 1.2, then it'll pull it in, right? Does this make sense? Head nods, yeah? So we're gonna cheat this a little bit. And instead of passing in a version number, we're gonna pass in a timestamp down to the seconds. So this means that every time WordPress loads a page, we have dynamically put in a new version number that's just like a second or a few seconds off, so it won't cache it so that we don't have to clear our caches we're developing.
[00:04:41] So WordPress has two hooks that we'll look at at one point as we get into here. It has a hook called WPHead and WPFooter, and behind the scenes it feeds everything in here. So if a theme is missing WPHead, it'll break a lot and if it's missing a tag of PHP called WPFooter, that's how it injects everything in the right place.
[00:05:01] So that's enqueue script, pretty straightforward and we will look at this so many times today that if this is not making sense now, it will be literally second nature by the time we get out of here, along with this other one called localized script.