It sounds like you’re doing it right. In a stock Sage theme, if your function is defined in helpers.php, it should work in filters.php. See the display_sidebar() function as an example.
Here’s another test that works:
helpers.php
function greet_daz()
{
return "Hello, Daz!";
}
filters.php
add_filter('the_content', function ($content) {
return $content . greet_daz();
});
Your answer helped lots as I forced me to see what I was actually doing wrong.
I was trying to separate filters.php into different files to keep the code more organised. I had created filters-checkout.php
filters-basket.php
filters-posts.php
etc
I tried including them in filters.php with include('filters-checkout.php');
but this didn’t work… I also tried the correct way of added new filter files in functions.php
but no function could be called from any new filter… file.
The issue in case anyone else has this, is to add the namespace to the top of your custom filter file: namespace App;
Yep, easy to forget the namespace declaration if you’re not in the habit. I personally have a snippet in VS Code that I use when I start all my PHP files that makes sure I have that in there as well as declare(strict_types=1);.