DEV Community

Cover image for What are hooks in WordPress?

Posted on

What are hooks in WordPress?

What are WordPress hooks?

WordPress hooks are a method for one piece of code to modify the behavior of another piece of code. They allow you to change how WordPress behaves without changing the core code itself. There are two types of hooks: actions and filters.

Types of WordPress hooks

There are two types of WordPress hooks: actions and filters.

Actions hook:
Actions hook allow you to insert additional code at specific points referred to as “hooks” throughout WordPress. For example, you can use an action to send an email or create a widget.

Filters hook:
Filters hooks allow you to modify existing code in WordPress. For example, you might use a filter to modify the content of a post before it is displayed or to change the post excerpt length of a WordPress site.

Here are some examples of action hooks:
wp_head: Executed within the <body> element of the page
wp_footer: Executed at the end of the page, before the </body> closing tag
publish_post: Executed when a post is published
delete_user: Executed when a user is deleted

And here are some examples of filter hooks:

the_title: Modifies the title of a post
the_content: Modifies the content of a post
wp_mail_from: Modifies the “from” address for emails sent by WordPress
excerpt_length: Modifies the length of post excerpts

There are many more action and filter hooks available in WordPress, and you can find a complete list in the WordPress Codex.

WordPress Hooks: A Complete Guide

Top comments (0)