The Ultimate Guide about the WordPress Hooks

WordPress Hooks are API (Application Programming Interface) which are provided by WordPress to allow your plugin to “hook-in” to your site; to call functions in your plugin when required and thus kick off the plugin.

Tech-babble aside, this is one of the reasons WordPress gives a lot of free reign to even the novice developers. Hooks allow you to change the behavior of code that isn’t yours without making any changes to it. This is how plugins work.

There are two types of wordpress hooks: Actions and Filters.

Actions are functions triggered by specific events. Filters are API though which WordPress passes data before it is supposed to do something else. Think of Action function as ‘doing’ or ‘executing’, and Filter function as ‘last minute editing before printing’.

Also Read:

WordPress typically does some filtering of its own. For instance, ‘the_title’ filter does its work every time WordPress outputs a post’s title. Now any plugin or theme that works on making that title look different can hook that filter and perform as their code instructs.

Where Does this Code Go?

For the kind of code we’re talking about here, there are two choices; You can add the code to your child theme functions.php file, or to a custom plugin (which goes to wp-content/plugins directory) you can create. Note that this does NOT include your parent theme (or any other theme) functions.php file. The whole point of wordpress hooks is to modify the behavior of code with changing it directly.

There are several advantages to adding this code to your custom plugin instead of child theme functions.php.

  • You can create small plugins for specific goals, which makes it easier to keep track of them.
  • You can use them on multiple sites.
  • Testing becomes easier when you have to enable/disable individual plugins instead of modifying the entire code.
  • The plugins stay with you even if you change a theme.

But it’s entirely up to you which method you use. Let’s begin with using Actions and Filters.

Note: In the following code examples, I have chosen the prefix ‘slug’. Make sure you always change it to unique slug for the plugin/theme you are working with.

Use of WordPress Hooks: Actions and Filters

Using Actions:

Think of Actions as the point where a piece of code takes a breather while another one runs.

Here’s what an action function hooked to WordPress looks like:

add_action( ‘wp_head’, ‘slug_change_h3_color’ );
function slug_change_h3_color() {
if ( is_category( ‘news’ ) || is_tag( ‘important-updates’) ) {
echo ‘<style>h3, h3 a { color: red;}</style>’;

The first line uses add_action() to hook/register the function  slug_change_h3_color()with WordPress. add_action() is a WordPress action function, and has two required parameters which tell the code WHEN to run (Example: ‘wp_head’) and WHAT to do (Example: ‘slug_change_h3_color’). The custom function slug_change_h3_color is the callback.

It’s not necessary to write the callback function immediately after your action hook. As long as it’s somewhere it can be executed, it’s good to go. I added it here because it makes for slightly-more-easily-digestible code.

The hook (first line of code) and the custom function slug_change_h3_color()go in the same .php file if you’re writing the code as part of a plugin. Make sure you install it correctly so it can be activated on admin section of WordPress.

There are loads of actions in WordPress core and even more are added by additional themes and plugins. Most commonly used in customized themes are wp_head and wp_footer. wp_enqueue_scripts is great for adding CSS and JavaScript files to your site.

Using Filters:

Filters are like actions, but they sit around waiting until they are called at a specific time to alter a data before it reaches its destination (read: completes execution).

Here’s what a hooked filter function looks like:

add_filter( ‘the_content’, ‘slug_add_copyright’ );
function slug_add_copyright( $content ) {
//get current post
global $post;

//create copyright notice using post date and site name
$date = get_the_date( ‘y’, $post->ID );
$copyright = ‘<p>Copyright ‘.$date.’ by ‘.get_bloginfo( ‘name’ );’.</p>’;

//append copyright notice to post content and return
$content = $content.$copyright;
return $content;


In the above example, add_filter() registers/hooks the function to WordPress and generates copyright before publishing the post on the browser. It’s similar to action function above, the only difference being that it provides us data for modifications. The function uses the data as a parameter. In the last line of this callback we return the variable. If we don’t, there will be no post –copyrighted or otherwise.

WordPress filters, like actions, are quite popular. the_content allows you to modify your content before it is output to the browser and published on your site for the general audience. A lot of social sharing plugins use this filter for great effect.

Also Read:

WordPress Plugin API (Actions and Filters) make for a vast topic that require comprehensive study and experimentation. I barely scratched the surface with this post. So check out WordPress codex for Actions and Filters, look up some more sources and tutorials about wordpress hooks, and start coding. It’s the only way to get the most out of one of WordPress more robust tools.

Author Bio: 

This is a guest post witten by Tracey Jones. She is a WordPress developer by profession and works with HireWPGeeks Ltd. She handles a team of experienced WordPress developers who are expert in providing WordPress customization service at very reasonable cost with 100% client satisfaction guarantee.

Muhammad Imran

Muhammad Imran is an enthusiast blogger and engineering graduate. He has good skills in WordPress and Blogspot, Blogging Tips, SEO, Make Money Online, Computer tips and also Creating tutorials. He loves to share inspiring and creative ideas with online community related to blogging.

Click Here to Leave a Comment Below 5 comments
Steve – IS A BIG CON – Beware!!
I contacted this company hirewpgeeks ltd and they gave me a quote to build my website, they wanted 50% upfront and promised to have it done in 22 working days. When the deadline came and went, they started blaming each other for xyz or even me for rushing them.
Two months went by with excuse after excuse and finally they stopped answering my calls, emails and skype. claim to be USA based on their website but I think they are all Indian based with US phone numbers. Its impossible to trace a Phonenumber listed to anyone other than the front man Chetan who answered the phone when I called. The so-called designer Ajay and Shushant the so-called project manager both don’t have phone numbers just skype addresses that they chose to reply to when it suits them.

I realise now that It was a big con and you must not believe the good reviews you may see on the internet, they are all written by Chetan, One of the reviewers you will see is a “Tracy Jones” and as I’ve found out, this is one of the aliases they use while advertising their company, the picture they have of her is a stock photo that you can get off the internet to use for any marketing campaign…

I think the scam is managed by Chetan, he writes tons of reviews and promises the world to unsuspecting victims like me…
You are welcome to write to me if you have any questions about my experience


    Muhammad Imran

    Hello Steve, this is a guest post. I haven’t used/tried their services yet. Will share a review about HireWPGeeks, if I use their service ever. Thanks for sharing your experience.


    Stop spamming. “Steve” is a profile created by one of our competitors for harming our reputation. I have checked all my past client requests and I never found anyone named “Steve”. We are a reputed WordPress web development company and provide outsource services. We respects out client’s needs and we are a trusted company with many happy clients all over the world.


I have question can you help me ?Recently I buy website from flippa. Seller said to me he transfered the wordpress blog to my hostagtor hosting. I want to know how can i access to wordpress login of my site. as after he is not reply me .. means how can find in hostagtor account my wordpress admin login and passward to access?

    Muhammad Imran

    Hello Alyia,
    First of all you must check following things;
    1. Is the site really transferred to your hosting account?
    2. The owner also transferred DB of the site?
    If both requirements are done then find the DB name in wp-config.php file. After finding the required Database name go ahead to phpMyAdmin, click on DB you find in wp-config.php file then search for wp_users table and just edit it. Here you can edit username, passwords, email etc. info. You just change the password, username and email as you wish.

    I hope it will help. If you face difficulties then feel free to contact me via contact form or send me your hosting login info, I will do it for you freely.


Leave a Reply:

Pin It on Pinterest

Share This