How To Use Action Hooks in WordPress




Geniuswp show

Summary: Action Hooks are a very useful tool in WordPress and they are used to perform functions (actions) in specific places of a theme or plugin. Many themes and plugins, such as Total, use action hooks as an easy way for users to modify the output of the project or to add their own custom code.1. do_action, add_action & remove_actionThere are 2 main functions you need to know to understand how hooks work:do_action() – where the “hooked” functions are runadd_action() – attaches a function to a hook as defined by do_actionremove_action() –removes a function attached to a specified action hookSo the do_action functions are already added to the Total theme and will run whenever something is hooked into them via add_action. So you, as the user, will be using the add_action and remove_action functions only to make your modifications.If you need a better explanation of thesefunctions please click on the links above so you can learn about them on the WordPress CODEX.2. Total Theme HooksThe Total WordPress theme includes many useful action “hooks” which will allow you to easily add/remove elements from the base theme layout. You can see a list of all the different hooks inside the theme at Total/inc/functions/hooks/hooks.php (screenshot below). Notice how these are all using the “do_action” function? That’s because these are all functions that will run in specific places of the theme.Remember the “do_action” function is going to run any functions that are “hooked”to it via the “add_action” function. For example (looking at the screenshot above) any function that is hooked into “wpex_hook_header_before” will run before the theme’s header code.3. Total Theme ActionsThe Total theme itself uses these various hooks for creating the general layout of the site. Have a look at Total/inc/functions/hooks/actions.php to see the default actions.Below is one of the functions you will find in that file:If you look at this example it’s pretty easy to see what’s going on due to the theme’s naming conventions.Basically you have various functions that will run on the specific action hooks.Now if you look at the file located at Total/inc/functions/hooks/partials.php you can see all the functions used by the action hooks. I have kept all the functions neatly organized in one place so you can quickly see all the hooked functions in one place, most of which simply get a specific template part with the code to display.4. Using Hooks For Theme Modifications?The theme hooks aren’t just there for the theme itself, they are also there for you! You can use any of the theme’s hooks to run your own functions or HTML – have a look at the “UberMenu Manual Integration” guide for a detailed example.Where Are... You are listening to the topic about "How To Use Action Hooks in WordPress", if you want to read the full article, please visit https://geniuswp.com or the link in the description.