What are “Action” and “Filter” hooks in WordPress?

Marc Wagner, September 5, 2024

In WordPress, hooks play a central role in the customization and extension of functionality without direct changes to the core code. There are two types of hooks: action hooks and filter hooks. Both allow developers to add custom functions at specific times or places in the WordPress process or to modify existing functions.

Action Hooks #

An action hook allows you to execute custom functions at specific points in the WordPress lifecycle. This means that you can “hook” a function that is called when this specific point is reached.

Example of an action hook #

function meine_custom_action() {
    echo '<p>Hallo, dies ist eine benutzerdefinierte Nachricht!</p>';
}
add_action('wp_footer', 'meine_custom_action');

In this example, we use the wp_footer action hook. This hook is executed just before the HTML tag </body> is rendered in the page. The meine_custom_action function adds a user-defined message in the footer of the page.

Important action hooks in WordPress #

  • wp_headIs called up in the <head> area of the theme.
  • wp_footerIs executed in the <footer> area of the page.
  • initIs triggered with every request in WordPress. Often used to register custom post types or taxonomies.
  • admin_initIs triggered during the initialization of the admin panel.

Filter Hooks #

A filter hook makes it possible to change data before it is output or further processed by WordPress. With filter hooks, you can dynamically adjust the content of texts, titles, metadata, etc.

Example of a filter hook #

function meine_custom_title_filter($title) {
    return $title . ' - Anpassung durch Filter';
}
add_filter('the_title', 'meine_custom_title_filter');

In this example, we use the the_title filter hook. This hook makes it possible to change the page title before it is displayed on the page. The meine_custom_title_filter function adds the text - Anpassung durch Filter to the original title.

Important filter hooks in WordPress #

  • the_content: Changes the content of a post or page.
  • the_excerptIs applied to the excerpt of a contribution.
  • the_title: Changes the title of a post or page.
  • wp_nav_menu_itemsAllows you to customize the menu items before output.

How to use action and filter hooks #

Registering a function with a hook: For both actions and filters, you use the WordPress functions add_action() and add_filter() to register your user-defined functions.

    add_action('hook_name', 'deine_funktion');
    add_filter('hook_name', 'deine_funktion');

    Arguments and priority: Both add_action() and add_filter() allow additional parameters to set the priority and determine how many arguments should be passed to the function.

    add_action('hook_name', 'deine_funktion', 10, 2);
    add_filter('hook_name', 'deine_funktion', 15, 1);

    The third parameter (e.g. 10 or 15) defines the priority. The smaller the number, the earlier the function is executed. The fourth parameter determines how many arguments are passed to the function.

    Conclusion #

    Hooks, whether action or filter, provide a flexible way to extend and customize the functionality of WordPress without changing the core code. Action hooks allow you to add new functions, while filter hooks manipulate the existing data before it is output.

    By using these hooks, you can customize and extend themes, plugins and WordPress itself in a variety of ways.

    Avatar of Marc Wagner
    Marc Wagner

    Hi Marc here. I'm the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.

    Similar Topics

    Comments

    Leave A Comment