Working With WordPress Hooks

Prasana
Timeless
Published in
3 min readDec 31, 2021

--

Hooks are an intrinsic part of WordPress development. Using hooks, you can directly affect the way your site’s code is executed and, as a result, functions. WordPress comes with a number of core hooks that you can use to add extra functionality to your site. This article will explain what WordPress hooks are and show you how to use them in your plugin development projects.

Hooks are the building blocks of WordPress. They allow you to modify the functionality of WordPress by adding code in different places. Most users will be interested in hooks that are used at specific times, such as when a post is published or a comment is made, but the real power is gained with hooks that can be used anywhere in WordPress. This means you can create your own plugins that are very specific.

What hooks do internally.

When creating our own hooks, we can use the do_action() and apply_filters() functions to execute all hooked functions internally. Using the WordPress source code as a guide, let’s explore how these work.

WordPress uses the function wp_trim_excerpt() to remove tags from excerpts. In WordPress, this function is located in wp-includes/formatting.php on line 2560. You should see the following line:

$excerpt_length = apply_filters( 'excerpt_length', 55 );

WordPress calls all functions hooked into its excerpt_length filter and returns the final value. For example, if you use three plugins that modify excerpt length, WordPress concatenates their results into a single string. what happens then? Let’s gather these functions in one place and take a look:

In the end, the excerpt length will be 104 words long when all of the plugins’ functions are considered. The priority of each function is in order from 3 to 2, with 3 executing first and 2 executing last. Therefore a new excerpt length value is set to 20 after plugin 3 executes its function. Next, plugin 1 kicks in and the length becomes 10. Finally, plugin 2 weighs in, making the length 104.

do_action() executes all functions tied to the hook defined as the first parameter.

Creating Hooks

As mentioned previously, all we have to do is make sure to use do_action() and/or add_filter and document it. Then anyone who has a need to know that this functionality exists will know it’s there.

Let us assume we will be creating a gallery of the most recent uploaded images pulled directly from the database, something like this:

We retrieve images ordered by date, limited to 10 results. Using the fields parameter I made sure that I get back an array of post ids to build a gallery shortcode out of this information.

There are two approaches you could take to add hooks here. You could add it simply before the definition of the $args array, like this:

In this case, you would need to use the $posts_per_page variable in the array. Perhaps a better solution would be to let the whole array be modified. With this approach, other plugins or you can add category, tag and other restrictions on the galleries here’s the full code.

Note: the underscore is not necessary at all. You could use my_plugin_query_args or any other format you wish. I prefer the underscore because it clearly states that this is a general plugin query arg and the functionality it provides.

That’s all there is to it. Other plugins can now use the my_gallery/query_args hook to modify the functionality of the galleries created.

Your feedback, suggestions, and thoughts are always welcome. write your response below.

--

--