What are hooks in wordpress? How to use wordPress hooks?

A Hook in wordpress refers to places where you can add your own code or change the default output of WordPress.

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 behaviour
  • can make changes easily. As lots of wordpress core functions use action and filter hooks so, once you understand the concept of wordperss 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 upgration.

In WordPress, there are two types of Hooks:

  1. Filter Hook
  2. Action Hook

1. Filter Hook

Filter hook allows you to change the output during the execution of wordpress. It means filter hook is used to manipulate the data.

  • Filter hook must return the result back for wordpress to use in its output processing.

Let’s understand by example –

Changing the excerpt length:

function excerpt_length_post($words)
return 20;

add_filter(‘excerpt_length’, excerpt_length_post’);

Change the url of the link in the logo:

function change_site_main_link()
return 'http://www.example.com';

add_filter('wp_logo_link_url', 'change_site_main_link');

2. Action Hook

Action hooks allow you to add functionality to the wordpress in order to get required functionality or customization.

  • Action hook is triggered at specific time while the wordpress is running and let you to take an action.

For example –

  • Executing some code when someone publishing a post.
  • Creating a widget when wordpress is initializing
  • Sending a tweet when someone publishes a post.

Action hook into Post Publishing :

function tweet_on_post_publish($post_id) 
      global $post;
      // Code to send a tweet with post info

add_action('publish_post', 'tweet_on_post_publish');

Action hook – widget Initialization :

function create_custom_widget() 
	 'name' => __( 'Custom Sidebar', 'custom_theme' ), 
	 'id' => 'custom_sidebar',
	 'description' => __( 'This is a custom theme', 'custom_theme' ),

add_action( 'widgets_init', 'create_custom_widget' );

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' );

Trending Topics