Different filter hooks might fire only once, or often multiple times. It just depends on what code is involved. Often the callback does not require much overhead, so multiple calls is of little detriment. Granted, 50-100 times is much more than I would expect, but it’s not unheard of.
If you need your callback to only execute once per request, you can have it remove itself from the call stack so it’ll only execute once on the first time through.
https://developer.ww.wp.xz.cn/reference/functions/remove_filter/
Thank you for your answer. I removed the callback, but to do so had to use a boolean variable ($done) to check if the function had been executed and put the remove_filter command outside the filter function (if ($done) remove_filter…). First, I tried to put the remove_filter command directly inside the filter function, before the return statement. Then the removal worked, but the filter itself didn’t work anymore.
That’s a reasonable solution, but removal shouldn’t affect other filter callbacks. Unless I’m misunderstanding the context. In certain context it is possible to completely disable the filter. But something like this normally works:
add_filter( 'option_active_plugins', 'disable_specific_plugin' );
function disable_specific_plugin($plugins){
// .........
remove_filter( 'option_active_plugins', 'disable_specific_plugin' );
return $plugins;
}
If this is what you did, I’m unclear why it breaks the filter, unless it’s the first and only callback added to that hook (but then with no other callbacks it’s of little consequence). I’m glad you came up with a workaround anyway.
You’re right, it also works when I remove the filter inside the function right before the return statement. Not sure, maybe I had some issues because I had combined several filters in one function. Now I have a separate function for each filter. Thanks again, you helped me a lot.