Use Action Hook in WordPress

How to Use Action Hook in WordPress

In WordPress source code, in addition to mastering WP Query to understand how it obtained its contents as described in the previous section, one of the crucial features to your other "communicate" with its source code which the Action Hook, in this article I will elaborate on the hook much action as possible.

What is Action Hook?

As in the last article, I have talked about the process that WordPress handles each visit. And Action Hook is an anchor point for us to perform an act of something at a certain cycle.

For example, you want our script will be executed after the source code has been downloaded to avoid errors caused by using some components in source code, then we'll hook it into the hook init scripts in WordPress.

How to use an Action Hook?

To use Action Hook in WordPress, we will have to create a PHP script should contain run (called a callback function), then use add_action function () in WordPress to your callback hook the hook on the need for its action hook into. Here's an example that I will run a hook to hook init function.

[php]function brt_theme_setup(){
// PHP Script here
}
add_action( 'init', 'brt_theme_setup' )[/php]

And this is not the template tag, so you can insert it in functions.php in your theme or plugin.

The structure used add_action function() is:

add_action ($ hook, $ function_to_add, $ priority, $ accepted_args);
Inside:
  • $hook (string) (required) - The name of the hook to hook into.
  • $function_to_add (callback) (required) - The name of the function should use.
  • $priority (integer) (optional) - The priority of enforcement functions to another function if it has the same hook. The default is 10, the smaller the more it done sooner.
  • $accepted_args (integer) (optional) - The number of parameters used in the callback function. The default is 1.

How to create Action Hook?

Action Hook do_action created with function(), this function is simply to declare an anchor point at the location of where it needs to execute. Due to the nature of it is just declare anchor point so it can be used as anchor points created action in a certain period, or you can add something on the spot of anchors.

For example, you can declare an action hook brt_before_content name in the template at any location with the following code (placed on the template of the theme):

[php]<?php do_action ('brt_before_content'); ?>[/php]

And now, if you want to insert something or execute something at the location that you've inserted the hook brt_before_content, just hook it to a callback function that is.

Some examples using Action Hook in WordPress

You already know what it was Hook Action and basic usage of it, now let's practice some common action in WordPress hook offline. You can consult the list of WordPress action hook for more.

This is the hook that you would use if you need to change the parameters of the query at a certain page in your callback function. This hook is called after the WordPress default query is created but before the query is run. This hook is usually used together with objects $query to correct the parameters of the query or do something to manipulate the default query.

For example, you want the WordPress hosting page will query retrieves a random article, I'll combine the functions of WordPress conditions and methods $query->set() in this hook.

The jaw condition when used in this hook will be written in the form of $query method. Example: $query-> is_home().

[php]</div></div>
<div class="line number4 index3 alt1">
<pre><div class="line">function brt_modify_archive_query( $query ) { //variable declaration to use the callback

if( $query->is_archive() && $query->is_main_query() ) : //is_main_query is used to avoid interference in the query is not the default
$query->set('orderby', 'rand'); // orderby=rand => random order posts
$query->set('posts_per_page', '1'); // posts_per_page=1 => display just 1 post in each page
endif;

}
add_action( 'pre_get_posts', 'brt_modify_archive_query' );</div>
<div class="line">[/php]

 So we did the work in every page customization query without touching the source code.

wp_head

This hook will be used if you want to insert something into the tags <head> section of the theme without modification of the theme template.

[php]</div></div>
<div class="line number4 index3 alt1">
<pre><div class="line">function brt_author_tag(){
echo "<link rel=\"author\" href=\"https://bricktheme.com\" />\n";
}
add_action( 'wp_head', 'brt_author_tag' );</div>
<div class="line">[/php]

If you need to insert the hook for footer, so we can use wp_footer.

Conclusion

In this article, perhaps you took a lot of knowledge about it and add_action in WordPress default then it is just like that, but if you write a plugin later, this feature extremely useful. Or when you use the theme framework hook it also uses a lot of action.