In my wordpress project I have seen several times the use of do_action()
function.
I don't know the working of this function, nor why we call it or where we use it?
Can someone please explain this to me?
In my wordpress project I have seen several times the use of do_action()
function.
I don't know the working of this function, nor why we call it or where we use it?
Can someone please explain this to me?
Check out the WordPress Codex Krishan: do_action
I quote from that codex page
Execute functions hooked on a specific action hook.
This function invokes all functions attached to action hook $tag. It is possible to create new action hooks by simply calling this function, specifying the name of the new hook using the $tag parameter. You can pass extra arguments to the hooks, much like you can with apply_filters(). This function works similar to apply_filters() with the exception that nothing is returned and only the functions or methods are called.
You can hook a function to an action hook using add_action().
There's different uses for the do_action hook in WordPress.
One way to use do_action is for creating new hooks which i have already written about.
https://wordpress.stackexchange.com/a/144883/9884
This enables you to add a custom hook in your parent themes template.
You can then use the custom hook in an add_action function in your child theme to hook in new content, a widget or a template tag for a slider as one example.
You can also add a conditional tag after the function for more control over which pages your function executes.
The custom hook can be used many times so you don't need to keep modifying your parent themes files.