A beginner’s guide to WordPress hooks: actions, filters and custom functions

The WordPress hooks are a way for developers to modify the default behavior of the WordPress platform by “hooking” into various points in the WordPress code. This allows developers to add their custom code to the platform without having to modify the core WordPress code.

WordPress hooks
WordPress hooks

There are two types of hooks in WordPress: actions and filters. Actions allow developers to add their code at a specific point in the WordPress code, while filters allow developers to modify data before it is displayed or saved to the database.

To use hooks in WordPress, developers can create custom functions and then use the add_action() or add_filter() functions to “hook” their custom function into the WordPress code.

Here is an example of how to use the add_action() function to create a custom function that displays a message on the WordPress login page:

function my_login_message() {
    echo "Welcome to our site! Please log in to continue.";
}
add_action( 'login_form', 'my_login_message' );

In this example, the my_login_message() function is the custom function that will display the message. The add_action() function is used to “hook” the custom function into the WordPress code at the login_form action hook. This will cause the message to be displayed on the login form.

Here is an example of how to use the add_filter() function to create a custom function that modifies the post title before it is saved to the database:

function my_modify_post_title( $title ) {
    $title = strtoupper( $title );
    return $title;
}
add_filter( 'title_save_pre', 'my_modify_post_title' );

1. What are Action hooks?

Action hooks allow developers to add their code at a specific point in the WordPress code. For example, an action hook might be used to add a custom script to the front end of the site or to send an email when a user registers.

Action hooks are triggered by specific events in the WordPress code, and developers can use the add_action() function to “hook” their custom function into the WordPress code at a specific action hook.

2. What are Filter hooks?

Filter hooks allow developers to modify data before it is displayed or saved to the database. For example, a filter hook might be used to modify the post title before it is saved to the database or to modify the excerpt length on archive pages.

Filter hooks are used to “filter” data as it is passed through the WordPress code, and developers can use the add_filter() function to “hook” their custom function into the WordPress code at a specific filter hook.

3. The difference between WordPress actions and filters

Both types of hooks can be used to create custom functions that modify the default behavior of the WordPress platform.

Action hooks allow developers to add custom code to WordPress at specific points in the code, while filter hooks allow developers to modify data as it is passed through the code.

4. Using a plugin to add custom WordPress functions

if you are creating a more complex modification that involves multiple hooks and custom functions, it may be easier to manage the code in a custom plugin.

Some of the advantages include:

  • Custom plugins can make it easier for developers to manage and update their custom code, as all of the custom functions and hooks are contained in a single plugin file. This can be especially useful for developers who are working on a team, as it allows them to easily share and collaborate on the custom code.
  • Using a custom plugin can help to improve the performance and security of the site by ensuring that the custom code is only loaded when it is needed, rather than being loaded on every page of the site. This can also help to reduce the risk of conflicts with other plugins or themes.

5. Examples of custom WordPress functions using hooks

These are just a few examples of the many custom functions that can be created using the WordPress hook system.

Adding a custom style sheet to the front end of the site:

function my_add_custom_style() {
    wp_enqueue_style( 'my-custom-style', get_template_directory_uri() . '/css/custom.css' );
}
add_action( 'wp_enqueue_scripts', 'my_add_custom_style' );

Modifying the excerpt length on archive pages:

function my_custom_excerpt_length( $length ) {
    return 50;
}
add_filter( 'excerpt_length', 'my_custom_excerpt_length', 999 );

Adding a custom column to the posts list table:

function my_custom_columns( $columns ) {
    $columns['custom_field'] = __( 'Custom Field', 'textdomain' );
    return $columns;
}
add_filter( 'manage_posts_columns', 'my_custom_columns' );

Modifying the default sort order of posts on the front end:

function my_custom_sort_order( $query ) {
    if ( is_home() && $query->is_main_query() ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'my_custom_sort_order' );

Adding a custom dashboard widget:

function my_custom_dashboard_widget() {
    wp_add_dashboard_widget( 'custom_dashboard_widget', __( 'Custom Widget', 'textdomain' ), 'my_custom_dashboard_widget_display' );
}
add_action( 'wp_dashboard_setup', 'my_custom_dashboard_widget' );

function my_custom_dashboard_widget_display() {
    // Display custom widget content
}

Sending an email when a user registers:

function my_user_register_notification( $user_id ) {
    $user = get_userdata( $user_id );
    wp_mail( 'admin@example.com', 'New User Registered', 'A new user has registered: ' . $user->user_login );
}
add_action( 'user_register', 'my_user_register_notification' );

Redirecting users to a custom page after login:

function my_login_redirect( $redirect_to, $request, $user ) {
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        if ( in_array( 'subscriber', $user->roles ) ) {
            return home_url( '/custom-page/' );
        } else {
            return $redirect_to;
        }
    }
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

Adding custom data to the user profile contact methods:

function my_custom_contact_methods( $methods ) {
    $methods['custom_field'] = 'Custom Field';
    return $methods;
}
add_filter( 'user_contactmethods', 'my_custom_contact_methods' );

Modifying the default address for WordPress emails:

function my_custom_wp_mail_from( $from_email ) {
    return 'custom@example.com';
}
add_filter( 'wp_mail_from', 'my_custom_wp_mail_from' );

Modifying the default from the name for WordPress emails:

function my_custom_wp_mail_from_name( $from_name ) {
    return 'Custom Name';
}
add_filter( 'wp_mail_from_name', 'my_custom_wp_mail_from_name' );

Modifying the default WordPress search query:

function my_custom_search_query( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array( 'post', 'page' ) );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'my_custom_search_query' );

Adding a custom body class to the front end of the site:

function my_custom_body_class( $classes ) {
    $classes[] = 'custom-class';
return $classes;
}
add_filter( 'body_class', 'my_custom_body_class' );

Modifying the default WordPress excerpt more text:

function my_custom_excerpt_more( $more ) {
return ' [Read more...]';
}
add_filter( 'excerpt_more', 'my_custom_excerpt_more' );

Adding a custom admin notice:

function my_custom_admin_notice() {
$screen = get_current_screen();
if ( $screen->id != 'dashboard' ) {
return;
}
?>
<div class="notice notice-info">
<p>Welcome to our site! Please be sure to visit the <a href="<?php echo admin_url( 'options-general.php' ); ?>">settings page</a> to configure your site.</p>
</div>
<?php
}
add_action( 'admin_notices', 'my_custom_admin_notice' );

Leave a Comment

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

Scroll to Top