• Resolved patrick0815

    (@patrick0815)


    Hi, to deactivate plugins for certain pages I added

    add_filter( ‘option_active_plugins’, ‘disable_specific_plugin’ );
    function disable_specific_plugin($plugins){ ………

    to functions.php in /wp-content/mu-plugins/.

    It works fine, I just wonder why this code is being executed so often. When for testing purposes I added some output, I noticed that depending on the page it’s being executed between 50 and 100 times. I would have expected it to be executed either once or once for each plugin. Is there an explanation for this (or maybe a way to avoid it and thus improve the performance of the site)? Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    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/

    Thread Starter patrick0815

    (@patrick0815)

    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.

    Moderator bcworkz

    (@bcworkz)

    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.

    Thread Starter patrick0815

    (@patrick0815)

    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.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘functions.php in /wp-content/mu-plugins/’ is closed to new replies.