Tuesday , 18 June 2019
Home / Programming / WordPress / Plugins / WordPress Action Hooks – WordPress Plugin Tutorial
WordPress Action Hooks
WordPress Action Hooks

WordPress Action Hooks – WordPress Plugin Tutorial

This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

Let’s create our first real plugin. We’ll be talking about WordPress action hooks and what hooks are.

WordPress Hooking System

WordPress has a very special hooking system that is the basis for creating plugins.  There are two types of hooks, actions and filters. We’ll only be discussing WordPress action hooks for this lesson.

An event is when something happens. This can be anything from a the footer loading or a comment is posted. When these events are triggered they allow any plugin to hook into this event and execute code. Different WordPress action hooks are triggered for different pages. There are tons of WordPress action hooks and all of them are listed at the WordPress codex.

The term hook comes from when an event is triggered, you’re able to hook into that event and do anything you want at that point. You can manipulate content, add content, update the database, change about anything in WordPress. The great thing is that you don’t even have to open any WordPress core files or anyone else’s plugins.

WordPress Action Hooks

Let’s get started, if you’ve been following my previous tutorials, then you should have a template folder in your plugins folder. Copy and paste this and then rename the new folder wpactionex.

Open up your index.php file let’s modify the header info to reflect our new plugin.

As you can see above, I’ve also added some additional comments below our header.  This is for our reference and to keep things organized in our plugin.  The first comment is where we’ll be placing any variables or functions we may need before we start anything.  Under our second comment, we’ll include any files that we may need. Our last and 3rd comment is where we’ll be adding our action and filter hooks.

At this point, you should activate your plugin.  To use WordPress action hooks is actually quite simple. We use the add_action() function provided by WordPress. The add_action() function has 4 parameters, 2 are required and 2 are optional.

The hook name is what we want to hook into. Take a look  here for a list of hooks we can use. The 2nd parameter, ‘your_function‘, is the name of the function we want to call when we hook into this action.  You’ll notice that we don’t have () after our function name. We don’t need this since because WordPress automatically knows it’s a function and that you don’t want to call it right away.

Our 3rd parameter, priority, is optional. This parameter tells WordPress if our plugin should be executed first before any other plugins hook into this action. By default, it’s set to 10. The reason, you may want to set this is because there may be multiple plugins using the same WordPress action hooks. If you believe your plugin should have first priority, then you can set this parameter to 1.  Our last parameter, args, is something we’ll discuss in a later lesson. This parameter is also optional.

We’re going to keep things minimal and simple, so, we’ll only be setting the first two parameters.  Back in our plugin, under the hooks comment, add this line of code.

As you can see, we’re hooking into the wp_footer action. This event is triggered when WordPress begins loading the footer of your page. Earlier, I gave a link to the list of actions we can use. Here’s what WordPress says about it.

The wp_footer action is triggered near the </body> tag of the user’s template by the wp_footer() function. Although this is theme-dependent, it is one of the most essential theme hooks, so it is fairly widely supported.

We gave WordPress the function jk_modify_footer. You’re probably wondering why I put jk_ before the function name. It’s absolutely important you give your functions, variables, and classes unique names. This is for the same reason why we have to give our plugin a unique name. Another plugin may be using the same names for their variables and functions. Getting both mixed up will cause errors. In this series, I’ll be prefixing everything with my name’s acronym, jk_.

If we refresh our WordPress home page and scroll down, you’ll see an error telling us that there is no function named jk_modify_footer.  Let’s fix that by defining our function. In our main index.php file, under our includes and requires comments, add the following code.

Earlier, I said we’ll be keeping everything organized. We’re not gonna define our function inside the main plugin file. Instead, we’ll create a new file called func.inc.php inside our inc folder. So, create that file right now. Inside that file, add this line of code.

Yes, we’re doing a Hello World example, but only because it’s so simple to understand. Save everything and refresh your page. You should see a little message saying Hello World! at the bottom of your page.

WordPress Action Hooks
WordPress Action Hooks

That’s it! Nothing to it right? This is the basics of using WordPress action hooks. You can go through the list of WordPress  action hooks available and mess around if you like. We use the add_action() function to allow us to hook into an action. We wanted to hook into the footer so we used the wp_footer. Once we’re hooked in, we want to call the function jk_modify_footer and then echo out Hello World! 


As you can see, WordPress is a very powerful CMS that can be extended into anything. In our next lesson, we’ll be talking about filter hooks. Similar to WordPress action hooks, they hook into WordPress and allow us to adjust WordPress to our liking. The main difference is that it allows us to modify the content rather than adding or running anything.

About Jasko Koyn

Check Also

WordPress Meta Boxes

WordPress Meta Boxes – WordPress Plugin Tutorial

WordPress meta boxes are used to display certain information or options in a page. It’s …

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.