In WordPress development process hooks are very useful. Look, to obtain any distinctive changes, you have to alter the core part,
but it is not good to touch core files, it will generate loopholes. Here hooks will help you.
Action Hook: It gives you a facility to perform your own task on particular situation.
Filter Hook: It does filtering, with the help of it you can change/modify the output.
You got the outline. Now below is list of some default hooks.
Action hooks
wp_head
archive_blog
dynamic_sidebar
the_post
widgets_init
Filter hooks:
bloginfo
body_class
comment_text
edit_posts_per_page
esc_html
excerpt_length
Why do we use hooks?
Answer is simple, to tell WordPress that you want to insert extra functionality.
How to create and use action hooks?
To learn this, Pay attention to the below mentioned procedure:
First of all create a function
Now there are two ways to implement this. Use it with default hook or use custom action hook for it.
For better explanation, take wp_head, it is used between <head> section of the theme to put your custom code like Css, JavaScript etc. Now link newly created function with this action hook.
On the other hand if you want to add a custom action hook, you can do it in three steps.
1. Define a function as we have done above.
2. Create a hook:
3. Link your function to this hook.
That's it, use this in your theme.
Mainly hooks are usable in theme especially for child theme and plugin development . It is an effective way to extend them, you can add any number of action hooks to make them more customizable.
I have tried to explain action hooks as easy as possible. Hopefully,you now understand well how it works and how can you apply it for your need. Just need to practice and there will be no confusion at all.
Other thing is Filter but the process is same with a little difference in implementation, I will jump onto it later.
What are WordPress Hooks?
Hooks are signals to inform WordPress about core or customize code. It allows to join new function with an existing function to update the core functionality without touching the base.Types:
Actions and filters are the two types of hooks are available. Now let's see what is the difference.Action Hook: It gives you a facility to perform your own task on particular situation.
Filter Hook: It does filtering, with the help of it you can change/modify the output.
You got the outline. Now below is list of some default hooks.
Action hooks
wp_head
archive_blog
dynamic_sidebar
the_post
widgets_init
Filter hooks:
bloginfo
body_class
comment_text
edit_posts_per_page
esc_html
excerpt_length
Why do we use hooks?
Answer is simple, to tell WordPress that you want to insert extra functionality.
How to create and use action hooks?
To learn this, Pay attention to the below mentioned procedure:
First of all create a function
function my_action()
{
echo $newstyle="<style> .example { background-color : #f1f1f1; } </style>";
}
Now there are two ways to implement this. Use it with default hook or use custom action hook for it.
For better explanation, take wp_head, it is used between <head> section of the theme to put your custom code like Css, JavaScript etc. Now link newly created function with this action hook.
add_action('wp_head', 'my_action');
On the other hand if you want to add a custom action hook, you can do it in three steps.
1. Define a function as we have done above.
2. Create a hook:
function new_hook()
{
do_action('custom_action_hook');
}
3. Link your function to this hook.
add_action('custom_action_hook', 'my_action');
That's it, use this in your theme.
<?php new_hook();?>
Mainly hooks are usable in theme especially for child theme and plugin development . It is an effective way to extend them, you can add any number of action hooks to make them more customizable.
I have tried to explain action hooks as easy as possible. Hopefully,you now understand well how it works and how can you apply it for your need. Just need to practice and there will be no confusion at all.
Other thing is Filter but the process is same with a little difference in implementation, I will jump onto it later.
0 Comment