The Talent500 Blog
Wordpress hooks and how they work 1

WordPress hooks and how they work

You can use WordPress hooks to alter the default operation of WordPress without altering the underlying files. Hooks can be utilized for both actions (action hooks) and filters (filter hooks).

Understanding hooks is important for anyone using WordPress. They assist you in creating custom features or customizing default settings for themes or plugins.

Cause of Hooks

The purpose of hooks is to automatically execute a function. Besides this process, It can alter, expand, or restrict the functionality of a theme or plugin.

Below is an example of a WordPress hook:

function mytheme_enqueue_script()

{wp_enqueue_script( ‘my-custom-js’, ‘custom.js’, false );}

add_action( ‘wp_enqueue_scripts’, ‘mytheme_enqueue_script’ );

The above examples show the hook was developed to link the mytheme_enqueue_script function with the wp_enqueue_scripts action. It activates a new action on your site; therefore, it is called an action hook.

Hooks are frequently used to make plugin components of an application. Hooks are used to build plugin components in content management systems (CMSs) such as WordPress, but they are also employed in eCommerce and intranet sites.

Hooks are divided into two groups: Action and Filter. The action hook is applied to add a process while the filter hook function is used to modify the value.

How to apply WordPress Hooks?

A bit of knowledge about HTML and PHP is required to use hooks in WordPress. It is not as difficult as you think to create action and filter hooks even if you are a complete beginner.

You only need to paste the hooks on your post page that you have copied from sites or created by yourself after switching to the text editor.

Create an Action Hook

You need to activate the add_action () function to add an action hook in the WordPress plugin. Activate this function by writing the below patterns in your functions.php file:

add_action(‘function name of target_hook’, ‘The_name_of_function_you_want_to_use’ ,’priority_scale’)

Hooks require the priority scale, as we can see above, to work effectively. This scale, which is based on a scale from 1 to 999, is automated and ordinal in nature. It specifies the order of priority for the functions connected to that specific hook.

A lower priority value suggests that the function will run sooner, whereas a higher priority number means that the function will execute later. The scale will display the installed functions’ output order when using the same target hooks.

The priority scale’s default value is 10. The scale may be set up based on how many target hooks you have.

Example of an Action Hook:

<?php
add_action( ‘wp_print_footer_scripts’, ‘hostinger_custom_footer_scripts’ );
function webhost_custom_footer_scripts(){
?>
<script>//fill the footer scripts right here</script>
<?php
}
?>

Create the Filter Hook

By utilizing apply_filters() function, create a filter hook by utilizing. This filter is used to modify, filter, or replace a value with a new one.

Example of a filter hook:

$score = 100;

echo “Current score is : “. apply_filters( ‘change_score’,

$score );

Below is the filter:

add_filter( ‘change_score’, ‘function_change_score’ );

function function_change_score( $score ){

? $score+=100;

? return $score;

}

The result should should be this:

Current score: 200

Unhooking the Actions and Filters

Use remove_action() and remove_filter() to disable the command from add_action() or add_filter() in your WordPress code.

These codes mainly serve as a means of excluding specific actions or filtering operations. It enables you to change a plugin that has too many pointless hooks and could interfere with the optimization of your website.

Here is an example of the remove_action() in WordPress:

remove_action( ‘wp_print_footer_scripts’, ‘hostinger_custom_footer_scripts’, 11 );

add_action( ‘wp_print_footer_scripts’, ‘hostinger_custom_footer_scripts_theme’, 11 );

function hostinger_custom_footer_scripts_theme()

{

?>

<script>//example of output by theme</script>

<?php

}

The above example demonstrates how to utilize the remove action to remove the built-in WordPress footer scripts and replace them with the specific footer script’s theme.

The command is applicable to all kinds of action hooks in WordPress.

Below is the example of  remove_filter:

remove_filter( ‘wp_mail’, ‘wp_staticize_emoji_for_email’ );

add_filter( ‘tiny_mce_plugins’, ‘disable_emojis_tinymce’ );

}

The example is to show how to deactivate wp_staticize_emoji_for_email which helps to convert emojis to static images.

It replaces them with disable_emojis_tinymce which will deactivate the emoji feature in WordPress (emoji is meant to slow down the site because it makes an extra HTTP request).

You can use the remove_filtercommand to disable multiple filters in a sequence.

Below is the example:

remove_action( ‘wp_head’, ‘print_emoji_detection_script’, 7 );

remove_action( ‘admin_print_scripts’, ‘print_emoji_detection_script’ );

remove_action( ‘wp_print_styles’, ‘print_emoji_styles’ );

remove_action( ‘admin_print_styles’, ‘print_emoji_styles’ );

remove_filter( ‘the_content_feed’, ‘wp_staticize_emoji’ );

remove_filter( ‘comment_text_rss’, ‘wp_staticize_emoji’ );

remove_filter( ‘wp_mail’, ‘wp_staticize_emoji_for_email’ );

add_filter( ‘tiny_mce_plugins’, ‘disable_emojis_tinymce’ );

add_action( ‘init’, ‘disable_emojis’ );

}

The above code seeks to fully disable WordPress’ emoji functionality. It demonstrates visibly that there is no restriction on the number of remove_filter statements that may be included in functions.php.

Practical Examples

As explained previously, you can use a variety of hooks to build a unique WordPress function.

Here are some of them:

admin_post_thumbnail_size

Your post’s thumbnail is displayed in the filter hook’s “Featured Image” section. Three variables are connected to the functions: $size, $thumbnail_id, and $post.

The hook should look like this:

$size = apply_filters( ‘admin_post_thumbnail_size’, $size, $thumbnail_id, $post );

According to the requirements, you change the $size parameter.

For example, if you would like to set the thumbnail size to 240 x 240 pixels, you can write this code:

$size = apply_filters( ‘admin_post_thumbnail_size’, 240, $thumbnail_id, $post);

By adding the array () function, you can also set a custom size for your thumbnail. The code will look like this:

$size = apply_filters( ‘admin_post_thumbnail_size’, array(240, 400), $thumbnail_id, $post);

The array () function sets your thumbnail to be displayed in 240 x 400 pixels.

after_password_reset

This action hook is triggered when a user resets their password. The hook consists of two parameters: $user and $new_pass.

The hook should look like this:

do_action( ‘after_password_reset’, $user, $new_pass );

customize_loaded_components

This hook acts as a filter to exclude some WordPress components from its core process. The functions that work on core files are wp-activate.php, wp-config-sample.php, or wp-settings.php. While the component is a collection of features on WordPress that represents one particular function in the widget.

It is important to note that customize_loaded_components cannot be added to a theme since it is only activated during the ‘plugins loaded’ phase.

The hook consists of two parameters: $components and $this. It should be written like this:

$components = apply_filters( ‘customize_loaded_components’, array( ‘widgets’, ‘nav_menus’ ), $this );

The $components parameter is a batch of core functions to load, while $this refers to the object in the existing class.

You can modify the array () function to determine which components to exclude. The above example shows that widgets and nav_menus are the components excluded from the core process.

Conclusion

Since there are many hooks that can be embedded in WordPress. Try to experiment using various types of hooks to manipulate the functions.

You can create your own plugins with this feature once you become proficient.

 

 

0
Subhojit Hazra

Subhojit Hazra

He is a tech enthusiast and a passionate marketer with an eye for detail. He loves to uncomplicate things and debate on business problems. A quiet guy who likes peaceful evenings and iced coffees.

Add comment