10 Most Used WordPress Hooks for Developers

10 most used wordpress hooks every developer should know

WordPress hooks were created to let developers customize and expand functionality without altering the core software.

Over time, they have evolved alongside WordPress into a powerful and flexible system of both action and filter hooks working together, allowing themes and plugins to seamlessly interact with the platform’s core processes.

10 Most Used WordPress Hooks for Developers

In this post, we’ll explore the 10 most used WordPress hooks for developers — including both action and filter hooks — the essential ones every professional should know to follow best practices and work the WordPress way.

1. init

Fires early in the WordPress loading process, after core components are initialized.

It is an action hook, meaning it allows you to execute custom code at a specific point in the WordPress workflow without modifying core files.

add_action( 'init', 'register_book_cpt' );
function register_book_cpt() {
    register_post_type( 'book', [
        'label' => 'Books',
        'public' => true,
        'has_archive' => true,
    ]);
}

Use of the init hook to register a “Books” custom post type, making it public and giving it its own archive page.

Use Cases:

  • Registering custom post types and taxonomies
  • Loading text domains for translations
  • Initializing plugin settings or global variables
  • Enqueueing scripts/styles early (though wp_enqueue_scripts is usually preferred for frontend assets)

WordPress Codex: init hook

2. plugins_loaded

Fires after all active plugins have been loaded, but before any pluggable functions are available.

This is an action hook, allowing developers to execute code that depends on other plugins being loaded, without modifying WordPress core

add_action( 'plugins_loaded', 'my_plugin_init' );
function my_plugin_init() {
        if ( class_exists( 'WooCommerce' ) ) {
        // Code that depends on another plugin
        My_Plugin_Class::do_something();
    }
}

Use plugins_loaded to run a function that checks if another plugin’s class exists. If the class is available, it executes dependent code safely after all plugins have loaded.

Use Cases:

  • Initializing plugin code that depends on other plugins
  • Loading translation files (load_plugin_textdomain)
  • Running compatibility checks between plugins

WordPress Codex: plugins_loaded hook

3. wp_enqueue_scripts

Used to properly register and enqueue frontend scripts and styles. Ensures that JS and CSS files load in the right order and without conflicts.

Fires when WordPress is ready to enqueue scripts and styles for the frontend only.

It is an action hook, allowing developers to add JavaScript and CSS files in a controlled and conflict-free manner.

add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );

    function my_theme_scripts() {
        wp_enqueue_style('my-style', get_template_directory_uri().'/                                                   style.css' );
    wp_enqueue_script( 'my-script', get_template_directory_uri() . '/script.js', array('jquery'), null, true );
    }

This example uses wp_enqueue_scripts to add a stylesheet and a JavaScript file, ensuring they load correctly and only once.

Use Cases:

  • Adding custom styles and scripts to themes or plugins
  • Loading third-party libraries like jQuery, Bootstrap, or FontAwesome
  • Ensuring dependency management and proper loading order of scripts
  • Optimizing frontend performance by enqueuing scripts in the footer

WordPress Codex: wp_enqueue_scripts hook

4. wp_head

Fires within the <head> section of the theme. It is an action hook that allows developers to insert scripts, styles, meta tags, or other elements into the page head.

add_action( 'wp_head', 'add_custom_meta' );
function add_custom_meta() {
       echo '<meta name="author" content="Your Name">';
}


The hook wp_head adds a custom meta tag in the page head section, allowing developers to insert elements that appear in <head>.

Use Cases:

  • Adding meta tags (SEO, author, viewport, etc.)
  • Inserting custom CSS or JS
  • Integrating third-party scripts (analytics, tracking pixels)
  • Adding Open Graph or social media tags

WordPress Codex: wp_head hook

5. the_content

Fires when WordPress retrieves the post content before it is displayed.

This is a filter hook, meaning it allows developers to modify or augment content dynamically without altering the original post in the database.

add_filter( 'the_content', 'add_custom_content' );
function add_custom_content( $content ) {
    if ( is_single() ) {
        $content .= '<p>Thank you for reading!</p>';
       }
    return $content;
}

Here, the_content filter hook is used to append a message at the end of single posts. The function receives the existing content, modifies it, and returns the new content to be displayed.

WordPress Codex: the_content hook

6. wp_footer

Fires just before the closing </body> tag in the theme. It is an action hook that allows developers to add scripts or HTML that should appear at the end of the page.

add_action( 'wp_footer', 'add_custom_footer_script' );
function add_custom_footer_script() {
    echo '<script>console.log("Footer script loaded");</script>';
}

This wp_footer hook will add a small JavaScript snippet at the bottom of the page, ensuring it runs after all other content has loaded.

Use Cases:

  • Adding custom JavaScript or tracking scripts
  • Injecting HTML content before </body>
  • Loading analytics or marketing scripts
  • Improving frontend performance by deferring scripts

WordPress Codex: wp_footer hook

7. the_title

Fires when WordPress retrieves a post or page title before it is displayed. It is a filter hook, allowing developers to modify titles dynamically without changing the original content in the database.

add_filter( 'the_title', 'prepend_custom_prefix' );
function prepend_custom_prefix( $title ) {
    if ( is_single() ) {
         $title = '★ ' . $title;
    }
    return $title;
}

In this code the_title filter hook will add a star prefix to single post titles. The function receives the original title, modifies it, and returns the new value to be displayed on the frontend.

Use Cases:

  • Adding prefixes or suffixes to titles (e.g., labels like “Featured”)
  • Modifying titles based on category, custom post type, or user role
  • Appending icons, badges, or dynamic markers
  • Formatting titles for SEO or readability

8. save_post

This hook fires whenever a post or page is created or updated in WordPress. It is an action hook, allowing developers to execute code immediately after content is saved, without altering core behavior.

add_action( 'save_post', 'save_custom_post_meta' );
function save_custom_post_meta( $post_id ) {
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
    if ( isset($_POST['custom_field']) ) {
        update_post_meta( $post_id, '_custom_field',     sanitize_text_field($_POST['custom_field']) );
    }
}

This example hooks into save_post to save a custom field whenever a post is saved. It checks for autosaves and sanitizes input before updating the post metadata.

Use Cases:

  • Saving custom fields or metadata for posts and pages
  • Triggering automated tasks after content is saved
  • Validating or modifying post data before it is finalized
  • Integrating with external APIs or logging systems upon content creation

WordPress Codex: save_post hook

9. wp_ajax_{action} / wp_ajax_nopriv_{action}

Fires when WordPress handles an AJAX request.

The {action} part is the trigger that executes the AJAX call. wp_ajax_{action} handles requests from logged-in users, while wp_ajax_nopriv_{action} handles requests from non-logged-in users (typically frontend).

These are action hooks that allow developers to create secure and structured AJAX endpoints

add_action( 'wp_ajax_my_custom_action', 'handle_ajax_request' );
add_action( 'wp_ajax_nopriv_my_custom_action', 'handle_ajax_request' );
function handle_ajax_request() {
    check_ajax_referer( 'my_nonce', 'nonce' );
    $data = isset($_POST['data']) ? sanitize_text_field($_POST['data']) : '';
    wp_send_json_success( [ 'received' => $data ] );
}

This example registers an AJAX handler for both authenticated and non-authenticated users. It verifies the nonce, sanitizes input, and returns a JSON response to the Javascript client.

WordPress Codex: wp_ajax hook

10. Custom Hook (e.g. my_plugin_cron_hook)

A user-defined hook created with do_action() or apply_filters(). Custom hooks allow modular and extensible code, and are often used together with wp_schedule_event() to run scheduled tasks

When used with WordPress Cron (wp_schedule_event()), custom hooks enable developers to schedule automated tasks without modifying core files.

// Schedule a custom cron event on plugin activation
register_activation_hook( FILE, 'my_plugin_activate' );
function my_plugin_activate() {
    if ( ! wp_next_scheduled( 'my_custom_cron_hook' ) ) {
    wp_schedule_event( time(), 'hourly', 'my_custom_cron_hook' );
    }
}

// Hook the custom function to the cron event
add_action( 'my_custom_cron_hook', 'my_custom_cron_function' );
function my_custom_cron_function() {
    // Example task: clear cache, sync data, or send emails
    update_option('Custom_cron_executed_at ' . current_time('mysql'));
}

This example defines a custom hook named my_custom_cron_hook and schedules it to run hourly.

The function my_custom_cron_function is triggered by the hook, allowing automated tasks like clearing cache, syncing data, or sending emails.

Useful Resources

Leave a Reply

Your email address will not be published. Required fields are marked *