0

How I can Activate and deactivate two plugins automatically at certain hours?

For example,

I want the "akismet" plugin to be active only from 10am to 7pm and the other Times (7:01pm to 9:59am) deactivated.

and

"Yoast Seo" plugin to be active only from 7:02pm-9:58am and the other Times (9:57am-7:03pm) deactivated.

Note: The reason for the one-minute difference between the activation/deactivation of two plugins is that if they are activated at the same time, they will cause errors and disturbances.

This code is Activate and deactivate on specific days and a plugin

$timestamp = time(); // Timestamp
$day       = date( 'D', $timestamp ); // Get day from timestamp
$active    = array( 'Mon', 'Wed', 'Fri', 'Sat' ); // Days plugin to be active
if ( in_array( $day, $active, true ) ) { // Yoast SEO is active
    activate_plugin( '/wordpress-seo/wp-seo.php' );
} else { // Yoast SEO is deactivated
    deactivate_plugins( '/wordpress-seo/wp-seo.php' );
}

How can I do it? Thanks.

3
  • 1
    why do you need the SEO and anti-spam plugins to be active partially? I am really curious in the reason behind this.
    – Eduard
    Aug 5 at 9:54
  • 2
    This is highly unusual. What are you hoping to accomplish with this? Do you actually need the plugins to be deactivated or are there just certain features you don’t want to be active during those times? Aug 5 at 11:45
  • akismet and Yoast Seo For example and Not Real.
    – user168547
    Aug 6 at 11:46

2 Answers 2

1

Rather than update the database you could write a mu-plugin to intercept reads from the active_plugins option, e.g.

function option_active_plugins_akismet_or_yoast( $plugins )
{
    if ( !defined('WP_ADMIN') ) {
        $hour = getdate()[ 'hours' ];
        if ( $hour >= 10 && $hour < 19 ) {
            $to_remove = 'wordpress-seo/wp-seo.php';
        } else {
            $to_remove = 'akismet/akismet.php';
        }
        $remove_index = array_search( $to_remove, $plugins );
        if ( $remove_index !== false ) {
            unset( $plugins[ $remove_index ] );
        }
    }
    return $plugins;
}
add_filter( 'option_active_plugins', 'option_active_plugins_akismet_or_yoast' );

This needs to go in a mu-plugin before the plugin load loop. This will leave both active in the admin site and because you're doing this at runtime there won't ever be any overlap when both are active. Unhelpfully there isn't a filter in wp_get_active_and_valid_plugins() to use instead.

I'm assuming these are regular plugins on a regular site and not multisite network-activated plugins. You might need to make the condition ( !defined( 'WP_ADMIN' ) && !wp_doing_ajax() ) too - I haven't thought about that too hard.

However this is a strange thing to do and you should really fix the conflict between your two plugins instead.

2
  • The above code does not work
    – user168547
    10 hours ago
  • Well the basic approach worked fine. This does work.
    – Rup
    9 hours ago
0

I was able to fix the interference problem and the code below works fine

$hour = getdate()[ 'hours' ];
if ( $hour >= 10 && $hour <= 19 ) { // Yoast SEO is active - akismet is deactivate
    activate_plugin( '/wordpress-seo/wp-seo.php' );
    deactivate_plugins( '/akismet/akismet.php' );
} else { // Yoast SEO is deactivated - akismet is activate
    deactivate_plugins( '/wordpress-seo/wp-seo.php' );
    activate_plugin( '/akismet/akismet.php' );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.