Useful WordPress Hook hacks

Useful WordPress Hook hacks

Online by JSC0d3
September 30, 2017 | | | | 1591 recognitions

Hooks are very useful in WordPress. They allow you to “hook” a custom function to an existing function, which allows you to modify WordPress’ functionality without editing core files. In this article, we have compiled 10 extremely useful ready-to-use WordPress hooks, along with examples and coding explanations.

What Is A Hook?

To achieve a particular effect on a WordPress blog, you have to modify how WordPress works. Some of these modifications are made to what WordPress developers call “core files,” files required by WordPress to work properly.

But modifying core files is always a bad idea. It may create a security loophole. Also, you will have lost the modification when you upgrade your WordPress installation.

Hooks are one of the main building blocks of WordPress plug-ins. Almost every plug-in uses a hook to overwrite WordPress’ core functionality.

How to Use Hooks

Unless you’re writing a plug-in, you would write hooks in the functions.php file. This file is located in the wp-content/themes/yourtheme directory.

A hook, as you would expect, “hooks” one function to another. For example, you could write a custom function and attach it to one of WordPress’ core functions:

add_action ( 'publish_post', 'callback_function' );

In this example, we hook our own custom function to WordPress’ publish-post function, which means that every time WordPress executes the publish-post function, it will also execute this hooked function.

Of course, we can also remove hooks using the remove_action() function:

remove_action ( 'publish_post', 'callback_function' );

How to detect the visitor’s browser using a hook

Cross-browser compatibility is probably the most common problem in Web development. You will save yourself a lot of headaches if you are able to detect the browsers that people use to visit your website and then create a custom class wrapped in the body tag. Few people are aware of it, but WordPress can already detect browsers, and a few global variables are available for us to use.

Nothing hard here: just paste the code below in your functions.php file, then save the file and you’re done!

add_filter( 'body_class', 'js_browser_body_class' );
if ( ! function_exists( 'js_browser_body_class' ) ) {
   * @param $classes
   * @return array
  function js_browser_body_class( $classes ) {
    global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

    if ( $is_lynx ) {
      $classes[] = 'lynx';
    } elseif ( $is_gecko ) {
      $classes[] = 'gecko';
    } elseif ( $is_opera ) {
      $classes[] = 'opera';
    } elseif ( $is_NS4 ) {
      $classes[] = 'ns4';
    } elseif ( $is_safari ) {
      $classes[] = 'safari';
    } elseif ( $is_chrome ) {
      $classes[] = 'chrome';
    } elseif ( $is_IE ) {
      $classes[] = 'ie';
    } else {
      $classes[] = 'unknown';

    if ( $is_iphone ) {
      $classes[] = 'iphone';

    return $classes;

Once you have saved the file, the function will automatically add a CSS class to the body tag. WordPress has global variables that return true if a visitor is using a particular browser. If the visitor’s browser is Google Chrome, the $is_chrome variable will return true. This is why we create the browser_body_class() function, which returns the name of the visitor’s browser. Once that’s done, we just hook the function to WordPress’ body_class() function.

How to insert content automatically after each post

Most blogs use the single.php template to insert some text, images or ads just after a post. Sure, this can be done by opening single.php and pasting the desired text after the the_content() function. But the text won’t show up in your RSS feed? Hooks and the technique described below solve this problem. One again, simply paste the code below in the functions.php file of your theme. That’s it.

add_filter( 'the_content', 'js_insert_foot_note' );
if ( ! function_exists( 'js_insert_foot_note' ) ) {
   * @param $content
   * @return string
  function js_insert_foot_note( $content ) {
    if ( ! is_feed() && ! is_home() ) {
      $content .= "<div class='subscribe'>";
      $content .= "<h4>Enjoyed this article?</h4>";
      $content .= "<p>Subscribe to our  <a href=''>RSS feed</a> and never miss a recipe!</p>";
      $content .= "</div>";

    return $content;

The purpose of the js_insert_foot_note() function is pretty simple: it simply concatenates text of your choice to the $content variable, which contains your post’s content.

Then, our js_insert_foot_note() function is hooked to the the_content() function, and it is automatically called every time the the_content is called. Using this function is basically the same as typing in the text at the end of each post.

Notice the (!is_feed) condition on line 2, which prevents the text from being inserted in the RSS feed. If you want the text to appear in your RSS feed, replace line 2 with the following:

if (!is_home()) {

How to prevent duplicate content on comment pages

Duplicate content is an SEO problem for many websites. It can also be a problem for your WordPress blog if you don’t apply a few tricks.

Introduced in version 2.7, paged comments is a great addition to WordPress because it lets bloggers split comments into multiple pages. The only problem, again, is the risk of duplicate content.

Let’s use the new rel="canonical" attribute to prevent duplicate content in paged comments. Simply paste the following code in your function.php file:

if( ! function_exists( 'js_code_canonical_for_comments' )){
   * Prevent duplicate content on comment pages
  function js_code_canonical_for_comments() {
    global $page, $post;
    if ( $page > 1 ) :
      echo "n";
      echo "<link rel='canonical' href='";
      echo get_permalink( $post->ID );
      echo "' />n";

add_action( 'wp_head', 'js_code_canonical_for_comments' );

First, we create a function to add the rel="canonical" attribute to comment pages, except page 1. Then we hook this function to WordPress’ wp_head() function. As simple as that!

How to use hooks and cron to schedule events

You probably already know that WordPress can schedule events. For example, it can publish posts at preset dates and times. Using hooks and wp-cron, we can easily schedule our own events. In this example, we will get our WordPress blog to send us an email once hourly. Paste the following code block in the functions.php file of your theme.

if ( ! wp_next_scheduled( 'js_task_hook' )) {
  wp_schedule_event( time(), 'hourly', 'js_task_hook' );

add_action( 'js_task_hook', 'js_task_cb_function' );

if( ! function_exists( 'js_task_cb_function' )){
  function js_task_cb_function() {
    wp_mail('', 'Automatic email', 'Hello, this is an automatically scheduled email from WordPress.');

The first thing we did, of course, was create a function that performs the desired action. In this example, the function is called js_task_cb_function() and it just sends an email to the specified email address.

To schedule an event, we have to use the wp_schedule_event() function. The last argument has to be a hook, which is why we hook our js_task_cb_function() function to js_task_hook.

How to list all hooked functions

When things go wrong, listing all hooked functions can be very useful for debugging. As with the others, this code has to be pasted in your functions.php file. When you have finished debugging, don’t forget to remove the code from functions.php, or else the debugging message will continue to appear.

if ( ! function_exists( 'js_list_hooked_cb_functions' ) ) {
   * @param bool $tag
  function js_list_hooked_cb_functions( $tag = false ) {
    global $wp_filter;
    if ( $tag ) {
      $hook[ $tag ] = $wp_filter[ $tag ];
      if ( ! is_array( $hook[ $tag ] ) ) {
        trigger_error( "Nothing found for '$tag' hook", E_USER_WARNING );

    } else {
      $hook = $wp_filter;
      ksort( $hook );
    echo '<pre>';
    foreach ( $hook as $tag => $priority ) {
      echo "<br />>>>>>t<strong>$tag</strong><br />";
      ksort( $priority );
      foreach ( $priority as $priority => $function ) {
        echo $priority;
        foreach ( $function as $name => $properties ) {
          echo "t$name<br />";
    echo '</pre>';


Once that’s done, simply call the js_list_hooked_cb_functions() function, as shown below, to print all hooked WordPress functions on your screen.


If no hook name is provided to the function as an argument, then hooks are printed to the screen using the global variable $wp_filter. Alternatively, you can list one particular hook by passing its name as an attribute:


Wrap up

Learning hooks is like studying law: You don’t need to know all of the statements and paragraphs, just where to find them. You will also never need all of them, but I hope you now understand the solutions posted in forums a bit better. Or you can get started by taking a closer look at any function that you are already using.

Let me know in the comments about your “hook” experience.

JSC0d3's Logo
About JSC0d3

JSC0d3 is an entrepreneur, online marketer, and an employee of an IT company. When not building websites, creating content, or helping customers improve their online business, spend time with their wife and two beautiful children. Although he still feels new in WordPress, he enjoys sharing what he has learned with all of you! If you want to get in touch with him, you can do this through this website.

On the same idea

Posted by | April 1, 2019

Images are vital components of every website Before you start questioning the importance of images, just try to imagine your favorite blog or website...

Posted by | March 5, 2019

I’ve been loosely following the noise and #wpdrama surrounding Gutenberg for as long as it has been around and honestly for the most part I’ve...

Posted by | February 24, 2019

To ensure that your site ranks highly in Search Engine Result Pages (SERPs), you’ll need to make it easy for search engine ‘bots’ to explore...

Previous PostBackNext Post
2 impressions on “Useful WordPress Hook hacks

Cristi Adam,

Thank you for info man, great article, it really fixed my issue

J&S Code,

We’re glad you enjoyed our website, thanks so much.

Leave here an impression