How to Add Actions and Filters
When using WP RSS Aggregator, or any other plugin for that matter, there are times when you will need to add a custom action or filter to your site for added functionality, or even to remove some.
While WordPress itself suggests using your theme’s functions.php
file for this, we recommend using a child theme or a third-party plugin that allow you to do this without modifying the parent theme files.
Here are a few reasons why this is recommended:
- The moment you upgrade your parent theme, the
functions.php
file is overwritten. So, whatever you added is suddenly gone, and you have to add it again from scratch on every theme update. - If you change your theme, you will need to add the same code to the new theme again.
If you are using a child theme, there is still the possibility of making a mistake in your PHP code, which could break the site and require FTP access to fix it. For this reason, we believe Method 1 below is the safest option.
Method 1: Code Snippets Plugin (Recommended)
To use this method, we recommend finding the filter you need from this knowledge base.
To install the plugin, follow these instructions.
- Once installed and activated, go to Snippets in your dashboard menu.
- Click on Add New.
- Add a Title, which could be the title of this article.
- Paste the code you copied in step 1 to the Code section.
- Add a Description or Tags if you wish to do so. It is not required.
- Click on Save Changes and Activate to save the filter and activate it.
- Or click on Save Changes to save the filter and activate it later.
- Your action or filter is now stored and active on your site.
Method 2: Use Your Child Theme’s functions.php File
The best way to do this is to use FTP access. This ensures that you can easily fix any mistakes easily and quickly, should you make any. If you do not know what FTP access is, please do not attempt to use this method.
- Access your site via FTP.
- Download your child theme’s
functions.php
file. - Keep a copy of the original file somewhere on your desktop.
- Open it in a code editor such as Sublime Text.
- Copy the code from our documentation to the bottom of the file.
- If the code snippet you are copying includes an opening PHP tag (
<?php
), do not copy it. - If there is a closing PHP tag (
?>
) at the bottom of yourfunctions.php
file, the code must be pasted above it.
- If the code snippet you are copying includes an opening PHP tag (
- Save the file.
- Re-upload it to your site via the FTP client, replacing the original file.
Your action or filter is now saved and live on your site. If the code has a mistake and it broke your site, repeat step 7, this time uploading the original file and replacing the modified one.