WordPress Theme Development Basic (Hooks)

Mosharrf Hossain
Mh Mohon
Published in
2 min readMar 19, 2019

What is Hooks?

Hooks are a way for us to add tow types of hooks our own code to customize in WordPress default WordPress behavior.

There are two types of hooks.

  • Action hooks— Let us run our own code when a certain event take place in the WordPress life cycle.
  • Filter hooks— Let us get data from WordPress, modify it and return it back customized.

Action Vs Filter hooks

Photo by Ian Wilson on slideshare

💎Action hooks

Action hooks are fired during a particular event or action which happens in the flow of a particular request of WordPress.

Using Action Hooks

There are three major hooks.

do_action() — To Create our own action hooks.

<?php //In some template
do_action('hook_name'); //Creates an action hook
do_action('hook_name', $arg, $arg)
?>

add_action() — Hook in code.

<?php // functions.php
add_action('action_name', 'callback_function', $priority, $arg);
?>

remove_action() — Unhook code.

<?php // functions.php
remove_action('action_name', 'callback_function', $priority);
?>

Example.

Suppose you are making a theme and you want that any plugin can customize and run something before the loop in your theme. You need to first create the hook:

<?php do_action( 'before_my_loop' ); ?>

Once you have added the above code any plugin can hook into your hook using the add_action function we saw above as below:

add_action( 'before_my_loop', 'add_comment_before_loop' );function add_comment_before_loop(){   echo "<b>Welcome to my blog</b>";}

Full Code:

💎Filter hooks

This hooks are fired generally before any content is saved or displayed on screen. One can add a function on a filter and alter the content which will be saved or displayed on the browser.

Photo by Ian Wilson on slideshare

Reference From: https://designmodo.com/wordpress-hooks-filters/

--

--