Hooks in WordPress. How to use WordPress hooks?

A hook is a generic term in wordpress that refers to addition of your own code with the wordpress core code and change in what wordpress is doing or outputting by default.

The main advantage of using hooks is, it allows you to modify or add features to WordPress without touching its core files.

Why you need to use hooks?

Hooks is absolutely necessary for everyone who else is doing development with wordpress. It is very important when you want to modify the wordpress theme’s or plugin’s behavior.
With the help of hooks, you:

  • can change the theme behavior
  • can make changes easily. As lots of WordPress core functions use action and filter hooks so, once you understand the concept of WordPress hook it becomes very easy job for you to understand the WordPress coding concept.
  • can write your own code to understand better and so it becomes easy to debug also.
  • can enable or disable the feature easily by making changes in functions.php
  • can make your website upgrade-proof as your code will not change with the change of WordPress core files during upgradation.

The position of hooks in wordpress code execution process plays an important factor.

There are two types of hooks exist in WordPress:

  1. Action
  2. Filter

What is Action Hook?

An action hook allows us to add some extra functionality to execute at a specific time when WordPress is running.

The Action hook is always triggered by an event in WordPress. WordPress doesn’t require any return values from your Action hook functions; the WordPress just inform your code that a specific event has been taken place.

For example – Adding extra menus or widget. Sending a promotional message for a post/page. Sending a tweet when someone publishes a post.

Syntax:

add_action($hook, $function_to_add, $priority, $accepted_args);

  • $hook: It will be the name of action hook in string. It is required parameter.
  • $function_to_add: The name of the function which you want to be called. It is required parameter.
  • $priority: Order in which the functions associated with a specific action are executed. Least numbers correspond to earlier execution. It is optional parameter. Default value is 10.
  • $accepted_args: specifies how many arguments the function will accepts. Default value is 1.

Let us understand with an example of action hook: Whenever a new post is published. Tell to your friends that you’ve written a new post.

function wp_new_post_email_friends( $post_id ) {
    $friends = 'deepakpandit.035@gmail.com, webdevdeepak@gmail.com';
    wp_mail(
        $friends, 
        'Explore new tutorial', 
        'I just put something new on my website: https://www.sharequery.com'
    );
 
    return $post_id;
}
add_action( 'publish_post', 'wp_new_post_email_friends' );

Action Hook used for adding Front-end Scripts and Styles :

function custom_styles() {
   wp_enqueue_style( 'bootstrap_css', get_template_directory_uri() . '/css/bootstrap.min.css' );
   wp_enqueue_style( 'main_css', get_template_directory_uri() . '/style.css' );
   wp_enqueue_script( 'bootstrap_js', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '', true );
   wp_enqueue_script( 'theme_js', get_template_directory_uri() . '/js/theme.js', array('jquery', 'bootstrap_js'), '', true );
  }
add_action( 'wp_enqueue_scripts', 'custom_styles' );

Action hook – widget Initialization :

function create_custom_widget() 
{
register_sidebar(array(
   'name' => __( 'Custom Sidebar', 'custom_theme' ), 
   'id' => 'custom_sidebar',
   'description' => __( 'This is a custom theme', 'custom_theme' ),
   ));
}
add_action( 'widgets_init', 'create_custom_widget' );

What is Filter Hook?

A filter hook is used to intercept or modify WordPress default data before it is send to the database or to the browser.

For example – Customizing how excerpts will be displayed. Inserting another CSS class in a WordPress HTML elements. Modify some of your page blocks.

Syntax:

add_filter($tag, $function_to_add, $priority, $accepted_args);

Let us understand Filter hook by an example: Modify the default excerpt length and display.

if(!function_exists( 'custom_excerpt_length'))
{
    function custom_excerpt_length($length)
    {
        return 40;
    }
}
add_filter('excerpt_length', 'custom_excerpt_length', 999);
Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial