Part 1: Actions - WordPress Hooks Tutorial For Beginners 2019

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this video I wanted to go over WordPress hooks now the word hook is just a blanket term that refers to places in your code where you can change what WordPress is doing by default now there are two types of hooks number one is actions which allows you to run code when an event happens in WordPress and number two is filters which allows you to modify data before it is either sent to the database displayed in the browser and so on and since this is a core concept of WordPress we will be spending some extra time not only going over how to use existing hooks but also how to create your own actions and your own filters let's get started with actions let's jump over to our functions PHP file when dealing with actions you're typically going to use two functions add action and do action add action is when you need to run code when something else is happening in WordPress and do action is used when you're creating your own custom actions so for right now let's deal with add action now there is a list online of all the different actions that exist in WordPress core one of those actions is called save post and the first parameter inside of this add action function is the name of the action that you want to hook into the second one is the function that you want to run when the first parameter happens so we want to run something called log wins saved so every time that somebody saves a post in WordPress this function is going to run so we just need to set up what that function is so a function log win saved and then so anything that we put inside of here will run when a post is saved and sometimes actions will actually pass a parameter to your function as well and in the case of save post it will give us the post ID so now we can pretty much do anything that we want to when that function is run so I thought a great example of how this works might be well how about every time that we save a post in WordPress we get the title of that post and then save it to a file so I have created a file called post log txt and what we want to have happen is just it to write out something like this post one was just saved post two that's three etc so in order to start that we're going to need to get that file path so post log is going to be at the stylesheet directory slash post log txt and then we're going to create a message this message is going to have the title of the post get the title post id so that's kind of what's cool about this is that action has given us the post ID to work with so we can pass that in to get the title and get the title of the post that was just saved it was just saved well let's do an exclamation mark and now we need to do some of the like writing of the file and all that and I went ahead and I just made the snippet for it so you'd have to watch all that so it just if the file exists opening it up and closed right to it and then close the file so let's see if this works if we go over here to adding a new post and say let's cross our fingers and hit publish we should be able to go back and go to our post log and you can see that we kind of got three lines here which is we don't really want that we just want it to come at once so why did it happen three times well the kind of tricky thing about this action is that it happens not only when you save a post or when you publish a post but it also happens when a post has been revised so when a post revision is created and when a draft has been created so in order to keep that from happening we're gonna write an if statement right here at the top so if is a post revision with then we'll pass that the post ID so it knows what we're talking about and then if it's that or if it is post autosave and pass that the post ID as well then we just want to return we just want it to do nothing and that should give us a more clean log file so let's take a look let's add a new file or a new post rather another test to publish and let's check our file again another test was just saved let's do another one for good measure another one for good measure publish and another one for good measure was just saved and now that that's looking good I want to move on to another scenario so in this scenario let's say that we have a page on our site and it's called super secret and we don't want anybody who's not logged in to WordPress to see this page so let's take a look here there is an action in WordPress called template redirect template redirect and we want to run a function called members-only so if we create that function called members only we can now run something inside of here now the difference between this action and that action is this one actually doesn't send us anything all it does is lets us know that WordPress is about to direct us to a template so we can interject here and say add some add some logic so we're going to say if it's a page that we're looking at and it's super secret and the user is not logged in so if both of those two things are true we want to do a WP redirect and we want to send them to the home page and then kill the process so if it's the page that we're talking about if it's super secret and they're logged and not logged in send them away if not they're fine to stay so let's test this out if we go to the page it should be super secret we are logged in so we can see it but if I log out and then go to that it will redirect us back to the home page so now we've taken a look at two actions that come with WordPress right out of the box let's take a look into creating our very own custom action so let's say for example we wanted to log every time that somebody visited our super-secret page and they weren't logged in so that logic is happening right here what we can do is we can use that function I was talking about earlier do action all we have to do is pass it a tag name and in this case user redirected now this could be anything you want we are naming this ourselves right now and then technically we can start using it I mean we can do add action user redirected and you know start our function well if we wanted to take it one step further we could actually pass arguments to this do action and receive them in our add action just like we were seeing up here when we were logging every time a post was saved we're we were receiving that post ID and this is where you would send across that variable so let's say for example we wanted to log not only that it was accessed but when it was accessed so all you have to do is just pass in an additional parameter and this can be anything and in this case we're gonna give it the date so it's gonna give us the day and time and all that and then we can pick that up inside of our add action so let's see here let's create our function it's going to be called log wind accessed and let's just create a function that's called exactly that and since we passed in the date we can now accept that here and so we're going to want this function to be pretty similar to the function that we had up here where we were saving the the post title so let's just copy and paste that and just make a few modifications so let's paste that now instead of post log we're gonna want this to be access log and instead of it saying that the post title was just saved let's just say that someone just tried to access our super secret page on and let's concatenate in the date so every time that somebody access that accesses that page or it tries to anyway we should write it to the access log so let's give that a try let's go back to our home page we're not logged in and let's try to go to super secret now it should redirect us back to the home page so that's still working and let's go back to our access log it looks like it worked we got our message along with the date and time so I hope you can start to see the power of actions they really are one of the main reasons as to why WordPress is so extendable and powerful and in the next video we're going to be going over filters and how they make WordPress even more powerful and extendable and like always if you have any questions feel free to leave a comment below if you enjoyed the video click like and subscribe so I know to keep doing what I'm doing hey guys I just wanted to give a quick shout out to Kin stuff if you are looking for hosting they are an awesome company that specializes in WordPress if you are on a another hosting and you're looking to upgrade definitely give these guys a look they will even migrate your site for free so you don't have to worry about transferring files and images and database and all that kind of stuff they will do it for you if you're running a small blog or a WooCommerce store they specialize in all that kind of stuff so give them a look the link is in the description and if you click it and sign up you'd be supporting me in the channel
Info
Channel: WPCasts
Views: 58,519
Rating: 4.9643655 out of 5
Keywords: WordPress Actions, WordPress Hooks, WordPress Development, WordPress PHP, WordPress Development Tutorial, WordPress Hooks Tutorial, WordPress Actions Tutorial, WordPress Actions For Beginners, WordPress Hooks For Beginners, hooks tutorial in wordpress, hooks and filters in wordpress, wordpress hooks and filters tutorial, wordpress tutorials for hooks and filters, wordpress hooks tutorial for beginners, complete wordpress hooks tutorials for beginners, wordpress tutorials
Id: 9GuJi8dYuAs
Channel Id: undefined
Length: 12min 10sec (730 seconds)
Published: Thu Mar 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.