How to use filters

WordPress is known for it’s flexibility, which is largely due to how it works with actions and filters. These are ways for developers to influence what is displayed where & how, or what operations to perform on certain moments or pages.

Our plugins include filters in key areas to give you further control over how the plugin functions or looks for your users. In most cases you’ll find a filter in our documentation, on a forum, or you’ll get it from a team member.

Caution: There is no ultra 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 theres 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 3 ways to add these code snippets to your site:

  1. With the Code Snippets plugin (recommended)
  2. In your theme options (recommended if available) – Some themes have a special area in the settings where you can enter custom code (not to be confused with custom styles/CSS) – this is always preferable over directly editing functions.php!
  3. Placing the code in your child theme’s functions.php file. This is the most direct and portable way and recommended for developers.

Code Snippets is a free and easy to use plugin that lets you add filters and actions fron 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:

  1. Go to Snippets > Add new
  2. 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.
  3. Copy paste / type the snippet, double check that you have not missed any characters.
  4. In the settings section, select where you want the snippet to be executed (everywhere, frontend or backend).
  5. Click “Save changes and activate” (just “Save changes” won’t do anything if you haven’t activated it yet).
  6. In case anything goes wrong, follow instructions from the faq on how to get your site up and running again.

The best way to prevent errors when placing a filter in your child theme’s functions.php is to:

  • Always make your changes via FTP (rather than via the WordPress editor) 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 it 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 a 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 affect may not show up immediately because of WordPress’s caching. If you don’t see it, try closing and re-opening your tabs for that site, or opening the page in a different browser.

 

Updates: Theme vs. Child Theme

You may have noticed that in the above we were talking about child theme functions.php. This is because whenever your theme gets an update, WordPress will overwrite functions.php with a new version and you’d lose your modifications. A child theme is basically a derivative of your main theme, in which you only specify what’s different to the main theme. Child themes never get updated! Read more about child themes here.