Create a custom filter hook in WordPress

WordPress has two main functions add_action() and add_filter() which mostly runs the WordPress core. Let’s discuss both functions and see how to create a custom filter hook in WordPress.

add_action()

As the name suggests itself this function adds action when a particular event triggers or at specific points of execution.

add_action() hook doesn’t return anything instead of that it adds extra functionality when a specific event occurs.

Let’s take one simple example, there is one action hook save_post is triggered when a post is saved in admin. So we can do some extra functionalities like sending that post data to an external CMS or API. That can be done with the action hook. See the below example.

add_action( 'save_post', 'blogex_when_post_save', 10, 3 );
function blogex_when_post_save( $post_id, $post, $update ) {
	// Your Code for other functionality
}

add_filter()

This function filters the default functionality of the WordPress core. WordPress gives the default data with any functions, and if you want to filter or override the data you can do with a filter hook.

add_filter() will always return something as it filters the data.

Let’s take one simple example of the_excerpt(). WordPress default provides 55 words in excerpts. So if we want to reduce or increase the word length you can override it.

In the below example, we filter the default excerpt word length to 25 words.

function blogex_excerpt_length( $length ) {
    return 25;
}
add_filter( 'excerpt_length', 'blogex_excerpt_length', 20 );

Create a custom filter hook in WordPress

We can use our custom filter hook by apply_filters() function. With the help of a filter hook, we can replace, update or change the value.

Default syntax:

add_filter( 'blogex_get_posts', 'get_needed_post', 10, 1 );

The above line of code is suggesting 4 things.

  • 'blogex_get_posts' is a hook name.
  • 'get_needed_post' is a callback function.
  • 10 is a priority of the function.
  • 1 is the number of arguments passed to the callback function.

For the priority things, It defines how fast your function is called. The lower number will execute earlier. 10 is the default priority number.

Let’s see one example.

add_filter( 'blogex_get_posts', 'get_needed_post', 10, 1 );
function get_needed_post( array $posts_array ) : array {
    $posts = [
        'page',
        'media',
        'attachment',
        'revision',
    ];

    return array_unique(
        array_merge(
            $posts,
            $posts_array
        )
    );
}

In the above example, we are getting the needed post types for our required functionalities. We can get all post types with WordPress’s default function. But we want only a few.

So we have created the filter hook. Now we want to get this list anywhere in our code, we can use apply_filters() function.

$all_posts = apply_filters( 'blogex_get_posts', ['post'] );

Make sure to add the second parameter as the same data type which you have defined in the function. In our case, we need $posts_array in the form of an array. It will be also a default value of the $all_posts.

So in the apply_filters() we have used ['post'] an array format. If we will add it as a string 'post' it will give an error.

Output:

Output

Hope you like the article!

Must Read:

How to write optimized WP_Query in WordPress?

Share this:

Related Post

How to contribute to a WordPress core in 2024?

How to contribute to a WordPress core in 2024?

How to write optimized WP_Query in WordPress[2 example]?

How to write optimized WP_Query in WordPress[2 example]?

Create a custom REST endpoint in WordPress[1 example]

Create a custom REST endpoint in WordPress[1 example]

How to create a custom shortcode in WordPress

How to create a custom shortcode in WordPress