Advanced WordPress Development

Master advanced WordPress development including REST API, Gutenberg, and performance optimization.

advanced Backend Development 6 hours

Chapter 3: WordPress Hooks and Filters

Chapter 3 of 15

Chapter 3: WordPress Hooks and Filters

3.1 Understanding Hooks

Hooks allow you to modify WordPress behavior without editing core files.

Hook Types:

  • Actions: Execute code at specific points
  • Filters: Modify data before it's used

3.2 Using Action Hooks

Action hooks execute code at specific points in WordPress execution.

// Add action hook
add_action('wp_head', 'my_custom_head_content');

function my_custom_head_content() {
    echo '<meta name="custom" content="value">';
}

3.3 Using Filter Hooks

Filter hooks modify data before it's used or displayed.

// Modify post title
add_filter('the_title', 'customize_post_title', 10, 2);

function customize_post_title($title, $post_id) {
    if (is_single()) {
        $title = '📌 ' . $title;
    }
    return $title;
}

3.4 Hook Priority and Arguments

Control hook execution order and pass arguments.

// Priority: Lower numbers execute first
add_action('init', 'first_function', 5);
add_action('init', 'second_function', 10);

// Multiple arguments
add_filter('the_content', 'modify_content', 10, 1);
function modify_content($content) {
    return $content . '<p>Additional content</p>';
}

3.5 Common WordPress Hooks

Frequently used hooks in WordPress development.

  • init: After WordPress loads
  • wp_enqueue_scripts: Enqueue scripts and styles
  • save_post: When post is saved
  • the_content: Filter post content
  • wp_head: Add content to <head>