Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plugins are getting deactivated after automatic plugin update in WordPress

Tags:

wordpress

When I manually triggered WordPress auto update using the wp_maybe_auto_update()function, after the auto update process completes the plugins are getting deactivated.

like image 317
Moh .S Avatar asked Oct 24 '25 21:10

Moh .S


1 Answers

I was experiencing the same problem, and digging into the code revealed the cause.

The Automatic_Upgrader uses the Plugin_Upgrader to update the plugins. The plugin upgrader has a method, deactivate_plugin_before_upgrade(), that is used to deactivate the plugin before upgrade, but only in specific cases. It contains these lines:

    // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
    if ( wp_doing_cron() )
        return $return;

So when the updates are being run by a cron job, the plugins don't get deactivated. The automatic updates normally are run via cron, and so the code has made an assumption that they always will be. If the automatic updates get triggered outside of cron (as when you are manually calling wp_maybe_auto_update(), then the plugins will get deactivated, but they won't automatically get reactivated.

One solution would be to trick the upgrader into thinking that cron was running, by hooking into the 'wp_doing_cron' filter before you call wp_maybe_auto_update():

add_filter( 'wp_doing_cron', '__return_true' );
wp_maybe_auto_update();
remove_filter( 'wp_doing_cron', '__return_true' );
like image 154
J.D. Avatar answered Oct 28 '25 06:10

J.D.