/

January 13, 2025

Understanding WordPress Hooks for Website Customization

Understanding WordPress Hooks for Website Customization.png

WordPress hooks are the backbone of customization in WordPress. They enable developers to add or modify functionality without directly editing core files. Hooks are categorized into two types: Actions and Filters. In this blog, we’ll explore 15 essential WordPress hooks that every developer should know to enhance their website development process.

What Are WordPress Hooks?
Hooks are functions in WordPress that allow you to “hook into” the core code to change or extend its behavior. Here’s a quick breakdown:
Actions: Allow you to add functionality at specific points.
Filters: Allow you to modify data before it is processed or displayed.

1. init

This hook executes prior to any output being sent to the browser, but after WordPress has finished loading. Custom post types and taxonomies are frequently registered using it.

				
					add_action('init', function() {
    register_post_type('custom_post', [
        'label' => 'Custom Post',
        'public' => true,
    ]);
});

				
			
2. wp_enqueue_scripts

To enqueue front-end styles and scripts, use this action.

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

				
			
3. admin_enqueue_scripts

Though tailored for the WordPress admin panel, this hook is comparable to wp_enqueue_scripts.

				
					add_action('admin_enqueue_scripts', function() {
    wp_enqueue_style('admin-style', get_template_directory_uri() . '/css/admin.css');
});

				
			
4. the_content

Before the post is shown on the front end, edit its content.

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

				
			
5. wp_head

Include custom code in your website’s <head> section.

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

				
			
6. wp_footer

Include scripts or other code in your website’s footer.

				
					add_action('wp_footer', function() {
    echo '<script>alert("Hello from the footer!");</script>';
});

				
			
7. login_enqueue_scripts

Make the login page unique by incorporating scripts or styles.

				
					add_action('login_enqueue_scripts', function() {
    wp_enqueue_style('login-style', get_stylesheet_directory_uri() . '/css/login.css');
});

				
			
8. wp_dashboard_setup

Add or remove widgets to personalize the WordPress dashboard.

				
					add_action('wp_dashboard_setup', function() {
    wp_add_dashboard_widget('custom_widget', 'Custom Widget', function() {
        echo 'Welcome to your custom dashboard!';
    });
});

				
			
9. save_post

When a post is saved, take action.

				
					add_action('save_post', function($post_id) {
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    update_post_meta($post_id, '_custom_meta_key', 'Custom Value');
});

				
			
10. rest_api_init

Modify or expand the REST API’s endpoints.

				
					add_action('rest_api_init', function() {
    register_rest_route('custom/v1', '/data', [
        'methods' => 'GET',
        'callback' => function() {
            return ['message' => 'Hello World!'];
        }
    ]);
});

				
			
11. pre_get_posts

Before the main query retrieves posts, make changes.

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

				
			
12. widgets_init

Register custom widgets for your site.

				
					add_action('widgets_init', function() {
    register_sidebar([
        'name' => 'Custom Sidebar',
        'id' => 'custom_sidebar',
        'before_widget' => '<div class="widget">',
        'after_widget' => '</div>',
    ]);
});

				
			
13. after_setup_theme

Run code, which is frequently used to configure theme support features, after the theme has been initialized.

				
					add_action('after_setup_theme', function() {
    add_theme_support('post-thumbnails');
});

				
			
14. login_redirect

After logging in, send users to a personalized page.

				
					add_filter('login_redirect', function($redirect_to, $request, $user) {
    return home_url('/dashboard');
}, 10, 3);

				
			
15. template_redirect

Before templates are loaded, change the behavior.

				
					add_action('template_redirect', function() {
    if (is_page('restricted')) {
        wp_redirect(home_url());
        exit;
    }
});

				
			
Conclusion

WordPress hooks are an invaluable device for builders to customize and enlarge WordPress functionality without touching middle files. By getting to know these 15 hooks, you could take your WordPress development competencies to the following level. Experiment with those hooks to your projects, and find out how they are able to rework your website’s functionality.

From the same category