Vajna Botond

Online Portofolio

Create WordPress custom hooks

When I create custom WordPress themes speed is a must. thaht`s one thing why you need custom themes, ready made themes have a lots of code you do not need in your site, and they are just slowing down your page load. For instance by avoiding the use of wp_head(); or/and wp_footer(); you can significantly speed up your page load, howewer, you still need to have your own scripts, and this is when I am using custom hooks.

To create a custom hook:

In functions.php create the hook:

function my_custom_hook(){
    do_action('my_custom_hook');
}

Add your functions to the hook:

function my_function1(){
    echo '<h1>Example Function1</h1>';
}
add_action('my_custom_hook','wp_function1',1);

function my_function1(){
    echo '<h1>Example Function1</h1>';
}
add_action('my_custom_hook','wp_function1',2);

Now you can add your hook anywhere in the theme to be executed:

<?php my_custom_hook();?>

More documentation on custom hooks you can find here:
https://make.wordpress.org/docs/plugin-developer-handbook/hooks/creating-custom-hooks/

One detailed tutorial on custom hooks you find here:
https://code.tutsplus.com/tutorials/adding-custom-hooks-in-wordpress-custom-actions–cms-26454