Articles on: Developers

How to use Hooks in Zakra Theme?

You can use hooks to add functionality to the Zakra theme. To add your custom code, you can use an action hook, and to modify any code, you can use a filter hook.


Additional Information: See WordPress Hook for more details

Zakra provides you with various hooks. To see all of them, you can visit here.

Using Action hook:

add_action( 'hook_name', 'function_to_add' );
function function_to_add() {
   //Your code goes here
}


In Zakra, you can use zakra_action_before_content hook to add your custom code just before the main content or body starts.

add_action( 'zakra_action_before_content', 'function_to_add' );
function function_to_add() {
   //Your code goes here
}


Using Filter hook

add_filter( 'zakra_action_before_content', 'function_to_add' );
function function_to_add() {
   //Your code goes here
}



Relevant Link: Example of Hook usage in Zakra Theme

All PHP code should be added via a child theme's functions.php file. Refer to Child Theme article to learn about child themes.


Modify theme functionality using Action Hook


do_action: Execute functions hooked on a specific action hook.

You only need to use these two functions:
add_action: Attach function to a specific hook.
remove_action: Removes function attached to a specific hook.

Note: do_action is already added to the theme and this function is automatically triggered whenever any function is hooked using add_action. You need to use add_action to add your code and remove_action to remove any functionality.

Updated on: 24/08/2022

Was this article helpful?

Share your feedback

Cancel

Thank you!