What are WordPress hooks? – Guide for beginners

Hooks, also known as hooks, are a feature that allows you to manipulate a procedure without modifying the file in WordPress core. A hook can be applied to actions (action hook) or filters (filter hook).

Learning about hooks is essential for any WP user. Hooks can help you create some functions or edit the default configuration of or .

Purpose of hooks

The main purpose of WordPress hooks is to automatically execute a function. Furthermore, this technique also has the ability to modify, extend or limit the functionality of a theme or plugin.

Here is an example of a hook in WordPress:

function mytheme_enqueue_script() {wp_enqueue_script( ‘my-custom-js’, ‘custom.js’, false );} add_action( ‘wp_enqueue_scripts’, ‘mytheme_enqueue_script’ );

The example above shows that the link is created to connect the function mytheme_enqueue_script with the action wp_enqueue_scripts. This hook triggers a new action on your site, hence the name action hook.

Hooks are often used to make plugins that complement an application. It is not only used in (CMS) like WordPress, but also commonly used in intranet and intranet sites within a company.

Also, as we mentioned earlier, WordPress hooks fall into two categories: action and filter. The action hook is used to add a process, while the filter hook is used to change or modify the value of a process.

How to use hooks in WordPress?

Using hooks in WordPress requires a bit of knowledge about WordPress and PHP. However, even if you are a beginner, creating action and filter hooks may not be as difficult as you think.

You just need to go to your page files and open the one you want to change with the text editor. When you are there, you can paste the hooks that you have copied from other sites or that you have created yourself.

See also  Case of Prof. Tomas Linkevicius: why home hosting is not an option

Create an action hook

To add an action hook, you must activate the function add_action() in the WordPress plugin. This function can be activated by writing the following patterns in your functions.php file:

add_action(‘the name of the target_hook function’, ‘the_name_of_the_function_you_want_to_use’ ,’priority_scale’)

As we see above, hooks use priority scaling to work properly. This scale is an automatic ordinal value based on a scale from 1 to 999. It defines the order precedence for functions associated with that particular hook.

A low priority value means that the function will be executed earlier, while the one with the higher value will be executed later. The scale will show the output sequence of the installed functions when using the same target_hooks.

The default value of priority_scale is 10. You can arrange the scale according to the number of target_hooks.

Here is an example of an action hook:

Note the pattern in the example above:

  • it is the place where you put the hook to work.
  • add_action is the command to create the action hook.
  • wp_print_footer_scripts is he target_hook which binds to a new function.
  • Hostinger_custom_footer_scripts is the function installed and linked to the target_hook
  • The example above shows that remove_action is used to remove the default WordPress footer scripts and replace them with 's custom footer scripts theme.

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

    Additionally, here is an example of remove_filter:

    remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' ); }

    The example above shows how to disable wp_staticize_emoji_for_emaila function that turns emojis into static images.

    It is then replaced with disable_emojis_tinymce which will disable the emoji feature in WordPress (emoji is known to slow down the site because it makes an additional HTTP request).

    Additionally, you can also use the command remove_filter to disable multiple filters in a sequence. Here is an example:

    function disable_emojis() { 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 aims to remove the emoji feature entirely in WordPress. It clearly illustrates that there are no limits on the number of commands remove_filter which you can embed in the file functions.php.

    Practical examples

    As we mentioned earlier, there are many hooks that you can use to create custom functions in WordPress. Here are some of them:

    admin_post_thumbnail_size

    This filter link displays a thumbnail image of your post under "Featured Image." There are three parameters that are connected with the functions: $size, $thumbnail_id Y $post.

    The hook should look like this:

    function disable_emojis() { 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' ); }

    Please note that you can change the parameter $size according to your needs. For example, if you want to set the thumbnail size to 240x240 pixels, you can write this code:

    $size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post );

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

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

    The function of array() above set your thumbnail to display at 240x400 pixels. You must find the best size that fits your page perfectly.

    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 the core process. By this we mean functions that run on core files, such as wp-activate.php, wp-config-sample.php either wp-settings.php. Whereas component is a collection of features in WordPress that represents a particular function in the widget.

    However, it is important to note that customize_loaded_components cannot be added to a topic, as it is only activated during the «« 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 );

    Parameter $components is a batch of main functions to load, while $this refers to the object in the existing class.

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

    conclusion

    To sum it all up, hooks are the functions used to change the default settings of your WordPress site. They allow you to add custom functions or disable processes without changing the main file.

    Hooks can be divided into two categories: action and filter.

    Although the methods of using these two are almost the same, they both have completely different functions.

    The action hook is used to create new functions, while the filter hook is used to modify existing code in the function.php file.

    Also, there are tons of hooks that you can insert into WordPress. try...

Loading Facebook Comments ...
Loading Disqus Comments ...