Menü schliessen
Created: March 17th 2025
Last updated: April 16th 2025
Categories: Wordpress
Author: Ian Walser

Top 10 Must-Know WordPress Hooks (Actions & Filters) Every Developer Should Master

Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Short Introduction

WordPress hooks—actions and filters—are the backbone of WordPress extensibility. If you're diving into WordPress development, whether it’s for themes, plugins, or customizing core functionality, understanding hooks is non-negotiable. In this guide, we'll walk you through the top 10 most essential hooks every WordPress developer—beginner or advanced—must know, complete with code examples and outputs.

What Are WordPress Hooks?

Hooks are points in the WordPress code where you can insert your own custom code. There are two types:

  • Actions: Let you add or modify behavior.
  • Filters: Let you modify data before it is sent to the browser or database.

Let’s explore the 10 must-know hooks in detail.

1. init – Initialize Your Code

The init action runs after WordPress has loaded but before any headers are sent. Ideal for initializing custom functions, post types, or taxonomies.

add_action('init', 'register_custom_post_type');

function register_custom_post_type() {
    register_post_type('book', [
        'label' => 'Books',
        'public' => true,
        'supports' => ['title', 'editor', 'thumbnail']
    ]);
}

The above code should create a new "Books" cpt.

2. wp_enqueue_scripts – Properly Add Styles and Scripts

This hook ensures your scripts and styles are enqueued correctly to avoid conflicts and keep things performant.

add_action('wp_enqueue_scripts', 'enqueue_theme_assets');

function enqueue_theme_assets() {
    wp_enqueue_style('main-style', get_stylesheet_uri());
    wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom.js', [], null, true);
}

The above code includes our CSS and JavaScript files properly in the theme.

3. the_content – Modify Post Content Before Display

A filter hook that lets you modify post content before it's rendered.

add_filter('the_content', 'append_read_more');

function append_read_more($content) {
    if (is_single()) {
        $content .= '<p><a href="#comments">Read More in the Comments</a></p>';
    }
    return $content;
}

A "Read More in the Comments" link appears after post content on single pages.

4. wp_footer – Inject Code Before the Closing </body> Tag

Use this to include analytics scripts, modals, or custom HTML in the site footer.

add_action('wp_footer', 'add_tracking_code');

function add_tracking_code() {
    echo '<script>console.log("Footer loaded");</script>';
}

Logs "Footer loaded" in the browser console.

5. admin_menu – Customize Admin Dashboard Menus

This action lets you add, remove, or rearrange admin menu items.

add_action('admin_menu', 'custom_admin_menu');

function custom_admin_menu() {
    add_menu_page('Custom Page', 'Custom Menu', 'manage_options', 'custom-page', 'custom_page_callback');
}

function custom_page_callback() {
    echo '<h1>Welcome to the Custom Admin Page!</h1>';
}

A new “Custom Menu” item appears in the admin panel.

6. login_enqueue_scripts – Style the Login Page

Perfect for branding the WordPress login screen.

add_action('login_enqueue_scripts', 'custom_login_styles');

function custom_login_styles() {
    echo '<style>body.login { background-color: #f0f0f0; }</style>';
}

The login background changes to light gray.

7. wp_head – Insert Code in the <head> Section

Used to add meta tags, favicon links, or other head-based content.

add_action('wp_head', 'add_custom_meta');

function add_custom_meta() {
    echo '<meta name="developer" content="John Doe">';
}

A developer meta tag is added in the page head.

8. comment_post – Trigger Actions When a Comment is Posted

This action lets you run code every time a new comment is submitted.

add_action('comment_post', 'notify_admin_on_comment');

function notify_admin_on_comment($comment_ID) {
    wp_mail(get_option('admin_email'), 'New Comment Alert', 'A new comment has been posted.');
}

Admin receives an email for every new comment posted.

9. user_register – Hook Into New User Registration

Trigger code when a new user account is created. Great for automation or onboarding emails.

add_action('user_register', 'welcome_new_user');

function welcome_new_user($user_id) {
    $user_info = get_userdata($user_id);
    wp_mail($user_info->user_email, 'Welcome!', 'Thanks for registering on our site.');
}

A welcome email is sent to the new user.

10. pre_get_posts – Customize Queries Without SQL

Modify the default WordPress query before posts are fetched. Great for altering archives, search results, and custom loops.

add_action('pre_get_posts', 'exclude_category_from_home');

function exclude_category_from_home($query) {
    if ($query->is_home() && $query->is_main_query()) {
        $query->set('cat', '-3'); // Exclude category with ID 3
    }
}

Posts from category ID 3 are hidden on the homepage.

Conclusion

Mastering these 10 hooks is like gaining a superpower in WordPress development. They let you inject custom functionality, extend WordPress core features, and build scalable, modular, and clean code. Whether you're building a plugin, a theme, or customizing an existing site, these hooks will be invaluable.