Could we help you? Please click the banners. We are young and desperately need the money
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.
Hooks are points in the WordPress code where you can insert your own custom code. There are two types:
Let’s explore the 10 must-know hooks in detail.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.