 Hello everyone. Sorry, it takes a bit so long here for me, but I'm ready now, so it's nice to see you all here, man. I'm seeing a couple of familiar faces, so that makes me a bit nervous. Okay, my name is Drep. I'm a web engineer at Tenna. Yeah, so where we make publishing easy or actually even fun. Today I'm going to talk to, I'm going to share to you part of how we make publishing somehow easy, but I have to warn you first that it's going to be a slightly sensitive topic. Who comes? Who are ready to talk about who comes? Not that kind of who comes, because we're going to talk about who comes? I mean who's in wordpress. That's what we're going to talk about today, not really the hook up that you know. Okay, so Dastun gave an overview in his talk earlier about registering a custom post type using an action hook. Did you see that? Did you see that? This is actually a follow-up talk on it. So Dastun, if you're here, thank you so much. So hooks. Hooks are part of the plugin API that refers to places where you can insert or change what wordpress is currently doing or outputting by default. To put it in a simple way, hooks allow you to intervene or possibly alter the result or execution. There are two kinds of hooks. There's actions and there's filters. Let's go ahead first with action. So actions are hooks that allow you to execute at a specific point during the runtime. This includes things like creating or initializing widgets, registering a custom post type or adding a menu. These are just a few of the many things you can do with actions. Filters on the other hand is the type of hooks that allow you to modify the result or data before it is used. You're allowing an external force here to contribute to the data before you use it further down in the execution. Yeah, so let's go right away to the anatomy of a wordpress hook. Actually, a wordpress hook can be called also as an execution point or a meetup point. By invoking this function, you are marking an area in your code where you allow others to hook up to process something. Anything hooked on in this action will be summoned or executed when the runtime reaches this execution point. The do underscore action is where it all begins. So when you see the red highlight there, that means it's required. It's not really part of the wordpress code or syntax. It's just my own desire to add that. So when you see the red highlight, that means a red flag. No, your edge is required. So the do action here is obviously a part of the action and action meaning. Next is the name of the action or event. And this is something you need to know in order to associate your function later on. It can be any name you want that makes sense to you. And as long as it has no spaces, as suggested by our wordpress on standard or best practices. Next one is the argument. And it's green. It's highlighted in green out because it's just optional. You can pass a single argument or more or even none at all. Whatever you feel like associated functions might be needing to do its necessary task. All in all, you can put it this way too. Hey, at this location where I put this do action, please execute all functions that are attached to this hook name. And also, please make these arguments available too. So you're kind of like commanding that part or execution point. I've got some examples here. These are with different implementation of do underscore action. We have do action with only the hook name as the parameter. We have another one with a hook name and a single argument or data. And the last one has hook name and three arguments. So that's how you can probably, that's how the variation of using do action. And those are all valid. Now that we know how to apply or mark a place in our code for those actions, let's look at adding an action. Adding an action is the act of hooking a function to an event where our event here is the hook name. To hook up, to hook up, not to the person, to hook up in our code, you need to simply invoke add underscore action. Then the hook name, in this case, it's my underscore action. This is the name of the action that tells what event your function should be associated with. And that function is the callback parameter. And it's mostly a function defined in your plugins and in your themes. It's basically the function that you would like to be processed. The next parameter is the priority number. Priority number is the number in which the function attached to an action are executed. If you haven't realized yet, we can hook up a number of functions to an action or event. Later on, you would want your function to get called first or in order for that to happen, you would add a lower number. Or if you want your callback function to get called later, you would add a higher number here and actually change the default. So if you're not going to add anything there, then we'll be passed to the add action function. And lastly, we have the argument count that simply identifies the number of arguments to pass to the callback function. And this should not exceed to the number of arguments given by the action. Remember that we are invoking or calling the due action earlier and there's arguments, those are only the limited number of arguments that you get passed to the function. By default, it's an argument count of one. I have different formats here of invoking and add action that we can look at. The first, let's start with how they differ in callbacks. The first one has normal callback format for procedural programming. We have an idea earlier by the talk of Jan Linux. He talked a bit about procedural versus object programming. The first example has a procedural approach. The second one is for a namespace callback functions. Namespace. So you don't mess with a namespace. And our last example here is if you are associating a class method as a callback. A class method needs to be in an array with an object instance as the first element followed by the method name. We also have different priorities in our example. Setting a lower number there would execute the callback earlier. That's the first example. Adding a 10 or no priority would be the normal order. In case that the hooks has the same priority number, they are executed in the order they were added in the action. And our last example has a higher number. That would mean this gets executed later. So if you want your function to be executed later and overwrite those coms before your function, you might want to add or to bump the number like 999 or go crazy like 999, or whatever. But as long as it's really higher. But make sense of it. Just control your function. How about if they have a different argument count? By default, you are passing one parameter which you may not use in your function. Sometimes in action, you may not use the parameter being passed in. The second is an example of passing in three arguments. And the third is actually valid. But really, passing 999 arguments to your callback function, that truly needs some code from you there. If you can, if you can add, well, if you can add, you can also remove. To remove an action, you only need to invoke remove underscore action and pass the name of the hook. Together with the same argument that you added the hook. Remember you added hook earlier, then you need to pass the same argument. It should have the same callback and the same priority number. So yeah, we have a couple of examples coming up. Who are Pokemon Go players here or hobbyists? Anyone? Hey, hey, don't, don't fool me. I mean, okay. Thank you to those who participate. I'm just curious, you know, but you might find some rare documents around here. So good luck. Okay, here we are trying to register a custom post side Pokemon. And registering a custom post side needs to execute early in the runtime. That's been talked about this earlier. So I'm just like, you know, repeating like some, some a bit of it. So we can make sure. So we need to make sure we hook up to the right action. This is very crucial WordPress that you need to hook up to the right action. There might be a lot of actions in WordPress, but you need to hook up to the right person I mean action. WordPress core has has a hook in place after the initialization stage where we can hook up our code. And that is the in it. So this is part of the WordPress core code. So they you can see how helpful, helpful documentation here because you can either go to WordPress quote code reference website to look for for to look at how to use it. Or you can simply rock on it right in your favorite ID. That's how I actually that's how I actually do most of the time. If I'm lazy to go to a website, I can just like go find a function right in the WordPress installation files, right? You can just like see some cool documentation. And so thanks to our core contributors who are like contributing documentation and make our lives easier. So now that we that we know where to hook, we want to we want them to associate our function Pokemon underscore in it, where we where we have our necessary codes to register the customer side. So yeah, in our Pokemon in it. So there you go. And viola you have a registered customer side by hooking into the action. He hooking into the action. There are tons of available action hooks in WordPress. There's in it, which is commonly used when registering a customer side and anything that needs to be executed earlier in the runtime. We have double P in it as well. We have double P head, which is commonly used to print third party script in the header as well as WP footer. If you'd like to place the script down in the footer, save post to process something after saving a post and add meta boxes, widgets in it for registering a widgets who doesn't like widgets. So now let's proceed with the filters. Let's take a quick look at the anatomy of a filter home. A filter will always return a value. That's the difference of this when it comes to the action hook. A filter will always return a value which then will be passed to another callback that is attached or that is hooked to the same filter. Or it can return back the value for use in the current page request. Here's where we are storing the final filtered result or value. Then we are officially calling the apply filter, apply underscore filters. This is what this is what allows the value to be filtered hook name is the first parameter as always. The record next parameter is the value being filtered. Of course, if you know if you want to filter something, you need something to be filtered. And an optional one or more parameter as always. Here are some implementation of our apply filters. You can simply pass the hook name and the value to the filter. Or you can add an argument as supporting data to hook's callback. Or you can add this directly as the expression in a conditional statement and see because since it can return a value. So there's something to validate there when you're adding it in a conditional statement. Adding a filter hook is pretty much the same as actually adding an action hook. This time we only have add underscore filter instead of add underscore action. And the rest has the same pattern as in add action. The callback, the priority number and the argument count. Still remember that? And it also has the same thing with removing a filter. And again, it must have the same argument. It's really important to have the same argument. Otherwise, the WordPress will read it as different than what you're trying to actually remove. Here's an example of filter hooks. We're filtering the post title here in the hope of making its title to be more Pokemon-ish. So again, it's Pokemon. So sorry. Yeah, so we will be using, in this case, we would want to use the underscore title that is readily available in WordPress and capture its value in your callback function. So here's a documentation of a black filters, that title that's already in place in the WordPress form. Then do the necessary operation from there in your function. And make sure to return the filtered value. So if the title, for example, is cat, then you would like cat to convert into meow or anything to be more Pokemon-ish, then you can do that. Or just the idea is you can always filter the title. Whatever your requirements in your project, whatever the requirements of your project would whatever needed in your project. Another example, if you're looking for a way to remove the CSS classes that are added by default when displaying menu in your template. You know, at first, when I work with displaying a menu, and then I feel like there's a bunch of classes being added there, and I'm not even using it. So I'm looking for a way to remove them for the semantic purposes of my HTML. So there's a nav menu CSS class filter is the way to go. You can add a filter. You can add a filter and assign a callback that that would return an empty string. That means no, that means no classes, no CSS classes. And that would get rid of those CSS classes for you. Or you can also achieve this with a helper function available in WordPress scores such as underscore underscore return underscore empty underscore string. I'm gonna feel like giving my address right. So there's available filter callback here return empty string, and that's right in the WordPress already. So you don't need to you don't need to write your own return return empty string callback for you to get rid of the empty string. There's already available WordPress. There's also return false return true return zero, and more of which are and more and a lot and a whole bunch more, which are intended as call filter callback function, provided by WordPress. So I suggest you find them like a Pokemon. No, they're pretty they're really rare, but you can always find them in our WordPress in the WordPress core. Again, there are tons that you can find in WordPress core. So please get to know them as much as possible and also utilize them whenever you can. We don't just end here by knowing how to add or how to remove an action. But there are also things you need. We need to keep in mind when working with hooks. A hook should have a descriptive descriptive descriptive name. It should clearly describe describe the event or location or data in a compressed format. For example, admin underscore in it. Yeah. So by seeing it, you'll know right away that whatever you hook up, whatever you hook up there would when whatever you hook up there would admin whatever you hooked up there will be will be fired when admin is initialized. Use hook to supplement an action, not the main action itself. It should not be dependent on the outcome of goods being executed from the hook. If a if any hook doesn't exist, it should not break your side at all. Knowing you can pass any number of argument doesn't warrant you to make all the things available to your callbacks. Only pass arguments that would make sense in the current context, and that would support the callback functions in accomplishing its task. And don't leave your developers friends hanging. Give them the best legacy you can ever leave behind by documenting every hook you have. And as you can see earlier, the admin in it, there's a documentation you can just simply read it and you know right away how to use it and what's the available argument that you can use. As important as knowing how to use hook is why we use it in the first place. Our plugins or teams can have codes spread all over the template, but hooks can can help you keep your logic separated. When working with a large code base deployed in a production environment, it's a very crucial moment to push features coming from a background of working with a VIP websites. Implementing a hook allows you to to more safely abstract a feature as a module or plugin and you can activate or deactivate it without breaking the site. And it makes your plugin more extendable, especially in a platform that becomes a very beautiful ecosystem. Thanks. And that's all. Thank you. Anybody has questions for you? Please be pretty sure. Anyone? No? Okay, maybe since we are late, maybe you can catch a little bit later. So yeah, thank you again. Our next