• Resolved Philipp Thom

    (@philipp-thom)


    I placed a hooked function into an mu-plugin.

    When I disable plugins on rest-api calls, those plugins get deactivated in wordpress globally also, not only during this specific request.

    I also noticed multiple calls of option_active_plugins. Then I only disabled plugins once, but with same result.

    Once I filter out any plugin from being loaded on specific page it gets deactivated globally.

    Is there any way to overcome this issue?
    Or is this complex due to the way how wordpress works?

    This is how the function logic looks like:

    
    $plugins_not_needed = array ("plugin-folder-name/plugin.php")
    foreach ( $plugins_not_needed as $plugin ) {
    	$key = array_search( $plugin, $plugins );
            if ( false !== $key ) {
    	   unset( $plugins[ $key ] );
    	}
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t recommend selectively deactivating plugins. Instead, let them always be active but manage what code executes based on your criteria. For example, to not execute code on API calls, do something like

    if ( ! defined('REST_REQUEST'))
       require_once('/path/to/non-api-code.php');
    Thread Starter Philipp Thom

    (@philipp-thom)

    @bcworkz Thank you for reply. If understand you right, this only works if all the installed plugins use this snippet in their code. So this is in theory likely to be the best solution. But how can this be achieved in production?

    Moderator bcworkz

    (@bcworkz)

    Yeah, my scheme only works for plugins you’ve written. I’ve misconstrued your situation. You’re removing plugins through the “option_active_plugins” filter, correct? That seems like it should work, except you don’t seem to be applying any conditions to distinguish API requests from others. Have you tried code similar to my suggestion for within plugins in your plugin removal code?

    It’s not unusual for filter hooks to be called more than once per request. It’s generally not a problem even if rather redundant. In the cases where it does cause trouble, we can have our callback remove itself after the first pass through to prevent further calls during the current request.

    If for some reason the “option_active_plugins” still causes trouble for you, most plugins modify how WP works through action and filter hooks. You could selectively remove key callbacks to disable the plugin without actually preventing it from being activated. It might be pretty tedious to seek out all the key callbacks, but it’s a more of an expected approach over preventing deactivation to start with.

    Thread Starter Philipp Thom

    (@philipp-thom)

    @bcworkz thank you again for your reply. You point me to a more reliable solution. I previously saw a similar post here, but without any satisfying answer.

    Now this should be also a good record for others getting to the same point.

    In meantime I found a plugin, that helps selectively deactivate plugins upon specific calls. https://ww.wp.xz.cn/plugins/freesoul-deactivate-plugins/

    I plan your suggestion for near future as enhancement.

    • This reply was modified 5 years, 1 month ago by Philipp Thom.
    • This reply was modified 5 years, 1 month ago by Philipp Thom.

    Hi, I’m experiencing the same issue. I’m using option_active_plugins to selectively disable plugins on certain pages, and it’s working on the frontend, but when used in the admin area, the plugins get deactivated permanently and globally. Any idea why this is?

    Thanks!

    Thread Starter Philipp Thom

    (@philipp-thom)

    @brentonklassen you see this behavior, because I suppose WP runs the hook multiple times. When WP checks currently active plugins in admin hook as you describe, it would update options table of active plugins on every page load.

    So a solution would be to check which hook is currently running inside your function.
    All backend hooks must then be skipped.

    I wasn’t able to do this.

    Maybe you can do this and share us your function.

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

The topic ‘option_active_plugins Filter disabling plugins’ is closed to new replies.