WordPress Hook

A WordPress hook is a powerful tool that allows developers to “hook into” the core WordPress system to modify or extend its behavior without changing the core files. Hooks make WordPress modular, extensible, and customizable.

There are two main types of hooks:

1. Action Hooks

These allow you to add or execute custom code at specific points in the WordPress execution lifecycle (e.g., after a post is published, or when the page footer loads).

add_action('wp_footer', 'my_custom_footer_message');

function my_custom_footer_message() {
    echo '<p>This is a custom message in the footer.</p>';
}

Explanation: This hook runs your function my_custom_footer_message just before the closing </body> tag of your site.

2. Filter Hooks

These let you modify data before it’s displayed or saved. WordPress passes the data through the filter, and you return the modified version.

add_filter('the_content', 'add_custom_text_to_content');

function add_custom_text_to_content($content) {
    return $content . '<p>Thanks for reading!</p>';
}

Explanation: This adds a custom paragraph at the end of every post content.

Why Use Hooks?

  • Modify themes/plugins without editing their files
  • Inject dynamic content (e.g. tracking codes, shortcodes, user info)
  • Interact with WordPress at specific moments in the page load or save cycle
  • Make your custom code update-safe and modular

Where to Place Hook Code?

  • In your theme’s functions.php file
  • In a custom plugin
  • In mu-plugins for must-use functionality