WordPress Tutorial
Lesson 41 of 50 (82%)
82% Complete

Introduction to WordPress Hooks

Learn how WordPress hooks allow developers to add functionality and modify website behavior without directly editing WordPress core files.

Learning Objective: Understand WordPress hooks, learn the difference between actions and filters, and use basic hook functions in PHP.

1. What Are WordPress Hooks?

WordPress hooks are points in the WordPress execution process where developers can run their own code or modify certain data. They help developers extend WordPress functionality without changing the original core files.

Hooks are commonly used in themes and plugins to add features, customize content, load scripts, modify titles, and perform other tasks.

Simple Example
Suppose you want to display a welcome message in the WordPress administration area. Instead of editing WordPress core files, you can use an appropriate action hook.

2. Why Are Hooks Important?

Benefits of WordPress Hooks
  • Add new functionality to WordPress.
  • Customize themes and plugins.
  • Modify content before it is displayed.
  • Run code at specific points during execution.
  • Avoid unnecessary changes to WordPress core files.
  • Make customizations easier to maintain.

3. Types of WordPress Hooks

WordPress has two main types of hooks:

Action Hooks

Action hooks allow you to execute your own code at a particular point in WordPress.

Example: add_action()

Filter Hooks

Filter hooks allow you to modify data and return the modified value.

Example: add_filter()

4. What Is an Action Hook?

An action hook allows a function to run when WordPress reaches a particular event or execution point.

For example, the admin_notices action can be used to display a notice in the WordPress administration area.

Example: Display an Admin Notice
<?php

function my_custom_admin_notice() {

    echo '<div class="notice notice-success is-dismissible">';
    echo '<p>Welcome to WordPress!</p>';
    echo '</div>';

}

add_action(
    'admin_notices',
    'my_custom_admin_notice'
);

Explanation:

  • my_custom_admin_notice() is our custom function.
  • admin_notices is the action hook.
  • add_action() connects the function to the hook.
  • The notice appears in the WordPress admin area when the hook runs.
Add this code to a suitable custom plugin or your child theme's functions.php file. It is intended for the administration area, not the public-facing website.

5. Syntax of add_action()

The basic syntax is:

add_action() Syntax
add_action(
    $hook_name,
    $callback,
    $priority,
    $accepted_args
);
Parameter Meaning
$hook_name The name of the action hook.
$callback The function or callback to execute.
$priority Controls the execution order. The default is 10.
$accepted_args The number of hook arguments accepted by the callback. The default is 1.

6. What Is a Filter Hook?

A filter hook lets you receive a value, modify it, and return the updated value. WordPress then uses the filtered value.

Example: Modify the Excerpt Length
<?php

function my_custom_excerpt_length($length) {

    return 20;

}

add_filter(
    'excerpt_length',
    'my_custom_excerpt_length'
);

Explanation:

  • excerpt_length is the filter hook.
  • $length receives the current excerpt length value.
  • return 20; supplies the modified value.
  • WordPress uses the filtered value when generating the excerpt.

The exact displayed excerpt length can depend on the theme or plugin and how it generates the excerpt.

7. Syntax of add_filter()

add_filter() Syntax
add_filter(
    $hook_name,
    $callback,
    $priority,
    $accepted_args
);

The parameters work similarly to add_action(). The important difference is that a filter callback normally receives a value and must return the value that WordPress should continue using.

8. Difference Between Actions and Filters

Action Hook Filter Hook
Runs code at a particular point. Modifies a value as it passes through a hook.
Commonly registered using add_action(). Commonly registered using add_filter().
Does not use a return value to modify hook data. Normally returns the modified value.
Example: admin_notices. Example: excerpt_length.
Useful for triggering tasks. Useful for changing content or settings values.

9. Common WordPress Action Hooks

Here are some useful action hooks for WordPress developers:

Hook Purpose
init Runs after WordPress has loaded but before headers are sent.
wp_enqueue_scripts Loads front-end scripts and styles.
admin_enqueue_scripts Loads scripts and styles in the admin area.
admin_notices Displays notices in the admin area.
wp_footer Runs near the end of the front-end page output.
save_post Runs when a post is saved or updated, subject to its hook behavior and arguments.

10. Common WordPress Filter Hooks

Hook Purpose
the_title Filters a post or page title.
the_content Filters post content before it is displayed through the relevant content pipeline.
the_excerpt Filters the post excerpt.
excerpt_length Filters the default excerpt word length.
body_class Filters the list of CSS classes applied to the body element.
document_title_parts Filters the parts used to build the document title.

11. Example: Add a Class to the Body

The body_class filter allows you to add a custom CSS class to the classes WordPress generates for the body element.

PHP Code
<?php

function my_custom_body_class($classes) {

    $classes[] = 'my-custom-page';

    return $classes;

}

add_filter(
    'body_class',
    'my_custom_body_class'
);

This adds my-custom-page to the body class list when the theme uses the WordPress body_class() template tag.

You can then style that class in your CSS:

CSS Code
.my-custom-page {
    background-color: #f4f6f9;
}

12. Hook Priority

Hook priority determines the order in which callbacks attached to the same hook run. A lower number runs earlier than a higher number.

Example
add_action(
    'init',
    'first_custom_function',
    5
);

add_action(
    'init',
    'second_custom_function',
    20
);

In this example, first_custom_function() runs before second_custom_function() when both callbacks are registered and the init hook executes.

13. Where Should You Write Hook Code?

Child Theme
Use the child theme's functions.php for custom functionality associated with that theme. Remember that the code stops being loaded when the child theme is no longer active.
Custom Plugin
Use a custom plugin for functionality that should continue working even if you change your WordPress theme.

14. Best Practices

Important Development Tips
  • Never modify WordPress core files to add hooks.
  • Use unique names for your custom functions.
  • Return the modified value from filter callbacks.
  • Choose the correct hook for the task.
  • Use appropriate escaping for output.
  • Validate and sanitize data when accepting user input.
  • Test custom hooks on a development or staging site.
  • Check callback arguments when using hooks that pass additional data.

15. Practical Assignment

Your Task
  1. Create a child theme or a simple custom plugin on a test website.
  2. Use the admin_notices action to display a welcome notice.
  3. Use the excerpt_length filter to change the default excerpt length.
  4. Use the body_class filter to add a custom class.
  5. Change hook priority and observe the execution order.
  6. Explain the difference between add_action() and add_filter().

16. Lesson Summary

  • Hooks provide extension points in WordPress.
  • Actions run callbacks at specific points.
  • Filters modify data and return the resulting value.
  • add_action() registers an action callback.
  • add_filter() registers a filter callback.
  • Priority controls the execution order of callbacks on a hook.
  • Hooks are commonly used in themes and plugins.

Lesson 41 Quiz: WordPress Hooks

Choose the correct answer for each question.

1. What are WordPress hooks used for?

2. Which function registers an action callback?

3. What should a filter callback normally do?