Skip to content

Latest commit

 

History

History
428 lines (331 loc) · 10.3 KB

File metadata and controls

428 lines (331 loc) · 10.3 KB

Most Useful Hooks & Filters Reference

Action Hooks (do_action)

WordPress Core Actions

Initialization & Setup

// Runs after WordPress has finished loading but before any headers are sent
add_action('init', 'my_init_function');

// Runs after WordPress has finished loading and the query has been parsed
add_action('wp', 'my_wp_function');

// Runs after the theme is loaded
add_action('after_setup_theme', 'my_theme_setup');

// Runs when WordPress is fully loaded
add_action('wp_loaded', 'my_wp_loaded_function');

// Runs after all plugins are loaded
add_action('plugins_loaded', 'my_plugins_loaded_function');

Frontend Actions

// Runs before the header is sent
add_action('wp_head', 'my_wp_head_function');

// Runs after the header is sent
add_action('wp_body_open', 'my_body_open_function');

// Runs before the footer is sent
add_action('wp_footer', 'my_wp_footer_function');

// Runs when the loop starts
add_action('loop_start', 'my_loop_start_function');

// Runs when the loop ends
add_action('loop_end', 'my_loop_end_function');

// Runs before the main query
add_action('pre_get_posts', 'my_pre_get_posts_function');

// Runs after the main query
add_action('wp', 'my_after_query_function');

Post Actions

// Runs when a post is created
add_action('wp_insert_post', 'my_post_inserted_function', 10, 3);

// Runs when a post is updated
add_action('wp_update_post', 'my_post_updated_function', 10, 3);

// Runs when a post is deleted
add_action('wp_delete_post', 'my_post_deleted_function', 10, 2);

// Runs when a post is published
add_action('publish_post', 'my_post_published_function');

// Runs when a post is saved (create or update)
add_action('save_post', 'my_save_post_function', 10, 3);

// Runs when a post is transitioned to a new status
add_action('transition_post_status', 'my_status_change_function', 10, 3);

User Actions

// Runs when a user logs in
add_action('wp_login', 'my_user_login_function', 10, 2);

// Runs when a user logs out
add_action('wp_logout', 'my_user_logout_function');

// Runs when a user is created
add_action('user_register', 'my_user_register_function');

// Runs when a user profile is updated
add_action('profile_update', 'my_profile_update_function');

// Runs when a user is deleted
add_action('delete_user', 'my_user_delete_function');

Admin Actions

// Runs in admin area
add_action('admin_init', 'my_admin_init_function');

// Runs in admin head
add_action('admin_head', 'my_admin_head_function');

// Runs in admin footer
add_action('admin_footer', 'my_admin_footer_function');

// Runs when admin menu is created
add_action('admin_menu', 'my_admin_menu_function');

// Runs when admin bar is rendered
add_action('admin_bar_menu', 'my_admin_bar_function', 999);

Plugin & Theme Actions

// Runs when a plugin is activated
add_action('activated_plugin', 'my_plugin_activated_function');

// Runs when a plugin is deactivated
add_action('deactivated_plugin', 'my_plugin_deactivated_function');

// Runs when a theme is switched
add_action('switch_theme', 'my_theme_switch_function');

// Runs when WordPress is updated
add_action('_core_updated_successfully', 'my_core_update_function');

Custom Actions

// Define a custom action
do_action('my_custom_action', $param1, $param2);

// Hook into custom action
add_action('my_custom_action', 'my_custom_function', 10, 2);

Filter Hooks (apply_filters)

WordPress Core Filters

Content Filters

// Filters the post content
add_filter('the_content', 'my_content_filter');

// Filters the post excerpt
add_filter('the_excerpt', 'my_excerpt_filter');

// Filters the post title
add_filter('the_title', 'my_title_filter');

// Filters the post permalink
add_filter('the_permalink', 'my_permalink_filter');

// Filters the post thumbnail
add_filter('post_thumbnail_html', 'my_thumbnail_filter');

Query Filters

// Filters the main query
add_filter('posts_where', 'my_where_filter');
add_filter('posts_join', 'my_join_filter');
add_filter('posts_orderby', 'my_orderby_filter');
add_filter('posts_groupby', 'my_groupby_filter');

// Filters the query arguments
add_filter('query_vars', 'my_query_vars_filter');

// Filters the posts results
add_filter('posts_results', 'my_posts_results_filter');

URL & Link Filters

// Filters the home URL
add_filter('home_url', 'my_home_url_filter');

// Filters the site URL
add_filter('site_url', 'my_site_url_filter');

// Filters the admin URL
add_filter('admin_url', 'my_admin_url_filter');

// Filters the login URL
add_filter('login_url', 'my_login_url_filter');

// Filters the logout URL
add_filter('logout_url', 'my_logout_url_filter');

Email Filters

// Filters the email headers
add_filter('wp_mail_headers', 'my_mail_headers_filter');

// Filters the email content
add_filter('wp_mail_content_type', 'my_mail_content_type_filter');

// Filters the email subject
add_filter('wp_mail_subject', 'my_mail_subject_filter');

// Filters the email body
add_filter('wp_mail_body', 'my_mail_body_filter');

User Filters

// Filters the user display name
add_filter('the_author', 'my_author_filter');

// Filters the user avatar
add_filter('get_avatar', 'my_avatar_filter');

// Filters the user capabilities
add_filter('user_has_cap', 'my_user_cap_filter', 10, 4);

// Filters the user roles
add_filter('editable_roles', 'my_roles_filter');

Admin Filters

// Filters the admin menu
add_filter('admin_menu', 'my_admin_menu_filter');

// Filters the admin bar
add_filter('admin_bar_menu', 'my_admin_bar_filter');

// Filters the admin footer text
add_filter('admin_footer_text', 'my_admin_footer_text_filter');

// Filters the admin notices
add_filter('admin_notices', 'my_admin_notices_filter');

Plugin & Theme Filters

// Filters the active plugins
add_filter('active_plugins', 'my_active_plugins_filter');

// Filters the plugin action links
add_filter('plugin_action_links', 'my_plugin_links_filter', 10, 2);

// Filters the theme directory
add_filter('theme_root', 'my_theme_root_filter');

// Filters the stylesheet directory
add_filter('stylesheet_directory', 'my_stylesheet_filter');

Custom Filters

// Define a custom filter
$result = apply_filters('my_custom_filter', $value, $param1, $param2);

// Hook into custom filter
add_filter('my_custom_filter', 'my_custom_filter_function', 10, 3);

Common Use Cases

Security & Validation

// Sanitize user input
add_filter('pre_user_login', 'sanitize_user');
add_filter('pre_user_email', 'sanitize_email');
add_filter('pre_user_url', 'esc_url_raw');

// Validate user capabilities
add_filter('user_has_cap', function($allcaps, $caps, $args, $user) {
    // Custom capability logic
    return $allcaps;
}, 10, 4);

Content Modification

// Add custom content to posts
add_filter('the_content', function($content) {
    if (is_single()) {
        $content .= '<div class="custom-content">Custom content here</div>';
    }
    return $content;
});

// Modify excerpt length
add_filter('excerpt_length', function($length) {
    return 20; // Return 20 words
});

// Modify excerpt more text
add_filter('excerpt_more', function($more) {
    return '... <a href="' . get_permalink() . '">Read More</a>';
});

Query Modification

// Modify main query
add_action('pre_get_posts', function($query) {
    if (!is_admin() && $query->is_main_query()) {
        if (is_home()) {
            $query->set('posts_per_page', 5);
        }
    }
});

// Add custom query vars
add_filter('query_vars', function($vars) {
    $vars[] = 'my_custom_var';
    return $vars;
});

Email Customization

// Customize email headers
add_filter('wp_mail_headers', function($headers) {
    $headers[] = 'X-Custom-Header: Custom Value';
    return $headers;
});

// Set email content type
add_filter('wp_mail_content_type', function($content_type) {
    return 'text/html';
});

Admin Customization

// Add custom admin menu
add_action('admin_menu', function() {
    add_menu_page(
        'My Plugin',
        'My Plugin',
        'manage_options',
        'my-plugin',
        'my_plugin_page'
    );
});

// Customize admin footer
add_filter('admin_footer_text', function($text) {
    return 'Custom footer text';
});

Priority & Arguments

Understanding Priority

// Higher priority (lower number) = runs earlier
add_action('init', 'function_1', 1);  // Runs first
add_action('init', 'function_2', 10); // Runs second (default)
add_action('init', 'function_3', 20); // Runs third

// Remove actions/filters
remove_action('init', 'function_2', 10);
remove_filter('the_content', 'function_2', 10);

Multiple Arguments

// Action with multiple arguments
add_action('my_action', 'my_function', 10, 3);

function my_function($arg1, $arg2, $arg3) {
    // Handle arguments
}

// Filter with multiple arguments
add_filter('my_filter', 'my_filter_function', 10, 3);

function my_filter_function($value, $arg1, $arg2) {
    // Modify and return value
    return $modified_value;
}

Best Practices

Performance

// Use conditional checks to avoid unnecessary processing
add_action('wp_head', function() {
    if (is_single()) {
        // Only run on single posts
    }
});

// Use transients for expensive operations
add_filter('my_expensive_filter', function($value) {
    $cached = get_transient('my_cached_value');
    if (false === $cached) {
        $cached = expensive_operation();
        set_transient('my_cached_value', $cached, HOUR_IN_SECONDS);
    }
    return $cached;
});

Security

// Always sanitize and validate data
add_filter('my_filter', function($value) {
    return sanitize_text_field($value);
});

// Check user capabilities
add_action('my_action', function() {
    if (!current_user_can('manage_options')) {
        return;
    }
    // Proceed with action
});

Debugging

// Debug hooks
add_action('all', function() {
    error_log('Hook fired: ' . current_filter());
});

// Debug filters
add_filter('all', function($value) {
    error_log('Filter applied: ' . current_filter() . ' - Value: ' . print_r($value, true));
    return $value;
});

Note: This reference covers the most commonly used hooks and filters. For a complete list, refer to the WordPress Plugin API Reference.