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

Marc Wagner, September 5, 2024

In Word­Press, hooks play a cen­tral role in the cus­to­miza­ti­on and exten­si­on of func­tion­a­li­ty wit­hout direct chan­ges to the core code. The­re are two types of hooks: action hooks and fil­ter hooks. Both allow deve­lo­pers to add cus­tom func­tions at spe­ci­fic times or places in the Word­Press pro­cess or to modi­fy exis­ting func­tions.

Action Hooks #

An action hook allows you to exe­cu­te cus­tom func­tions at spe­ci­fic points in the Word­Press life­cy­cle. This means that you can “hook” a func­tion that is cal­led when this spe­ci­fic point is rea­ched.

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 exam­p­le, we use the wp_footer action hook. This hook is exe­cu­ted just befo­re the HTML tag </body> is ren­de­red in the page. The meine_custom_action func­tion adds a user-defi­ned mes­sa­ge in the foo­ter of the page.

Important action hooks in WordPress #

  • wp_headIs cal­led up in the <head> area of the the­me.
  • wp_footerIs exe­cu­ted in the <footer> area of the page.
  • initIs trig­ge­red with every request in Word­Press. Often used to regis­ter cus­tom post types or taxo­no­mies.
  • admin_initIs trig­ge­red during the initia­liza­ti­on of the admin panel.

Filter Hooks #

A fil­ter hook makes it pos­si­ble to chan­ge data befo­re it is out­put or fur­ther pro­ces­sed by Word­Press. With fil­ter hooks, you can dyna­mi­cal­ly adjust the con­tent of texts, titles, meta­da­ta, 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 exam­p­le, we use the the_title fil­ter hook. This hook makes it pos­si­ble to chan­ge the page title befo­re it is dis­play­ed on the page. The meine_custom_title_filter func­tion adds the text - Anpassung durch Filter to the ori­gi­nal title.

Important filter hooks in WordPress #

  • the_content: Chan­ges the con­tent of a post or page.
  • the_excerptIs appli­ed to the excerpt of a con­tri­bu­ti­on.
  • the_title: Chan­ges the title of a post or page.
  • wp_nav_menu_itemsAllows you to cus­to­mi­ze the menu items befo­re out­put.

How to use action and filter hooks #

Regis­tering a func­tion with a hook: For both actions and fil­ters, you use the Word­Press func­tions add_action() and add_filter() to regis­ter your user-defi­ned func­tions.

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

    Argu­ments and prio­ri­ty: Both add_action() and add_filter() allow addi­tio­nal para­me­ters to set the prio­ri­ty and deter­mi­ne how many argu­ments should be pas­sed to the func­tion.

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

    The third para­me­ter (e.g. 10 or 15) defi­nes the prio­ri­ty. The smal­ler the num­ber, the ear­lier the func­tion is exe­cu­ted. The fourth para­me­ter deter­mi­nes how many argu­ments are pas­sed to the func­tion.

    Conclusion #

    Hooks, whe­ther action or fil­ter, pro­vi­de a fle­xi­ble way to extend and cus­to­mi­ze the func­tion­a­li­ty of Word­Press wit­hout chan­ging the core code. Action hooks allow you to add new func­tions, while fil­ter hooks mani­pu­la­te the exis­ting data befo­re it is out­put.

    By using the­se hooks, you can cus­to­mi­ze and extend the­mes, plug­ins and Word­Press its­elf in a varie­ty 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