How to use actions & filters hook? – Woo Invoice
How to use actions & filters
In WordPress development, Hooks are functions that can be applied to an Action or a Filter in WordPress. Actions and Filters in WordPress are functions that can be modified by developers to change the default WordPress functionality.
Functions used to modify Actions/Filters in WordPress can be hooked into WordPress. However, it is important to note that actions and filters are not the same things. Actions are functions performed when a certain event occurs in WordPress. Filters allow you to modify certain functions. Arguments used to hook both filters and actions look the same. But they are different in functionality and how they behave.
Our plugins include actions & filters in key areas to give you further control over how the plugin functions or looks for your users.
Warning: There is no safe method to do these things. If you can just copy-paste the code, you’ll usually be good. But if you need to make changes yourself and there’s a risk of a typo, or if you accidentally missed a character when copy-pasting, you can break your site. Of course, when you know this you can take precautions, which will outline below.
There are 2 ways to add these code snippets to your site:
- With the Code Snippets plugin (recommended)
- Placing the code in your child theme’s functions.php file. This is the most direct and portable way and recommended for developers.
Using Code Snippets
Code Snippets is a free and easy to use a plugin that lets you add filters and actions from your site backend without editing any files and also keep it organized and easy to manage. This doesn’t mean that you’re 100% safe from errors: if you make a copy-paste error that breaks the code, it could still crash your site, but compared to using functions.php there are fewer things that can go wrong and it’s much easier.
Here’s how to add a filter or action to your site with Code Snippets:
- Go to Snippets > Add new
- Give your new snippet a meaningful name and description. This will help later when you may have forgotten what the code was for, I also recommend putting a link to where you got the snippet.
- Copy-paste / type the snippet, double-check that you have not missed any characters.
- In the settings section, select where you want the snippet to be executed (everywhere, frontend or backend).
- Click “Save changes and activate” (just “Save changes” won’t do anything if you haven’t activated it yet).
- In case anything goes wrong, follow instructions from the faq on how to get your site up and running again.
The proper way to placing a hook is in your child theme’s functions.php. Because whenever your main or parent theme gets an update, WordPress will overwrite functions.php with a new version and you’d lose your modifications. A child theme inherits the look and feel of the parent theme and all of its functions, but can be used to make modifications to your WordPress. Follow the steps below to prevent error:
- Always make your changes via FTP (rather than via the WordPress editor or using any file manager plugin) and make sure you have a backup of the file in case something goes wrong.
- functions.php is located in wp-content/themes/your-child-theme/functions.php. make sure to read the top of that file, some themes use another file for custom functions and will tell you not to use that file.
- Place the codes at the very end of the file.
- but before the last ?> this is the PHP closing tag and there should be anything after that (not even space or a return). For this reason, the closing tag is usually omitted at the end of the file (odd as it may sound, this is considered good practice) – in that case, you don’t have to worry about this.
Finally, a filter’s effect may not show up immediately because of WordPress’s caching. So clear your browser cache, or opening the page in a different browser.