How To Use Action Hooks
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 Real Estate 7, 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_action
There are 2 main functions you need to know to understand how hooks work:
- do_action() – where the “hooked” functions are run
- add_action() – attaches a function to a hook as defined by do_action
- remove_action() – removes a function attached to a specified action hook
So the do_action functions are already added to the Real Estate 7 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 these functions please click on the links above so you can learn about them on the WordPress CODEX.
2. WP Pro Real Estate Theme Hooks
The Real Estate 7 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 realestate-7/admin/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.
3. 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.
Where Are My Custom Hook Functions Added?
When using the hooks to modify the theme you’ll want to add your code in the functions.php file of your child theme. You can generate a child theme easily via the admin in Appearance > Child Theme.
What Hook Should I Use?
When you look at the various hooks in the hooks.php file you will notice they have very obvious names for example the hook name “ct_before_header” is going to be a hook that is added before the theme’s header. If you have any doubts you can always look up the name of the hook (by searching the theme files) and see where it’s being added in the theme’s code.
4. Adding An Action (Example)
Hopefully, by now you understand what action hooks are and how they work. However, if you are still a bit confused hopefully an example will help!
Add Extra Text Under Your Header
A good example of using an action hook may be adding extra text under your header. This could be useful for adding a banner to your site, advertisement in the header, maybe an alternative logo (to display on mobile devices – by using CSS to show/hide each one accordingly), adding a quote…etc.
4. Overriding Parent Theme Functions within Your Child Theme
Say you wanted to change the Property Type Icons you see displayed in the lower-left corner of the listing images. You can easily overwrite a parent theme function within your child theme, by adding the example below into your child themes functions.php.