When using Aggregator, or any other WordPress 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 so important:
- 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 the below method is the safest and most reliable option.
Learn more about using custom functions in WordPress from WP Mayor.
The Code Snippets Plugin
Code Snippets is a free and widely used plugin for adding custom actions and filters to your site through a user-friendly and easily manageable interface.
To install the plugin, follow these instructions.
- Once installed and activated, go to
Snippetsin your Dashboard. - Click on
Add New. - Add a Title, which could be the name of the action or filter.
- Paste the custom code into the Code section.
- Add a Description or Tags if you wish to do so, though it’s not required.
- Click on
Save Changes and Activateto save the filter and enable it immediately. - Or click on
Save Changesto save the filter and activate it later. - Your action or filter is now saved and active on your site.
Note: Be careful not to have duplicate filters activated at the same time through the Code Snippets plugin. In some cases, this may cause a fatal error to occur. If that does happen, please refer to this article to recover your website.
Alternative: Child Theme
The best way to do this is to use SFTP access through an FTP client such as Filezilla. This ensures that you can easily fix any mistakes, should you make any.
IMPORTANT: If you do not know what SFTP access is, please do not use this method.
Here’s how it works.
- Access your site via SFTP.
- Download your child theme’s
functions.phpfile. - Keep a copy of the original file somewhere on your desktop.
- Open it in a code editor such as Sublime Text.
- Copy the custom code (action or filter) to the bottom of the file.
- If the code snippet you are copying includes an opening PHP tag (which looks like this:
<?php) at the bottom of your functions.php file, the code must be pasted below it. - If there is a closing PHP tag (which looks like this:
?>) at the bottom of yourfunctions.phpfile, the code must be pasted above it.
- If the code snippet you are copying includes an opening PHP tag (which looks like this:
- 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.