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 deactivated.


"Yoast Seo" plugin to be active only from 7pm-10am and the other Times deactivated.


1- akismet and Yoast Seo For example and Not Real.

2- omits the fact that function.php will not run code unless the site is hit. This could cause some potential misses. The other answer here using wp_schedule_event() uses CRON, and does not rely on "outside" forces.

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.

  • 1
    why do you need the SEO and anti-spam plugins to be active partially? I am really curious in the reason behind this.
    – RwkY
    Aug 5, 2022 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, 2022 at 11:45
  • akismet and Yoast Seo For example and Not Real.
    – user168547
    Aug 6, 2022 at 11:46

1 Answer 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.

  • The above code does not work
    – user168547
    Aug 11, 2022 at 23:09
  • Well the basic approach worked fine. This does work.
    – Rup
    Aug 12, 2022 at 0:06
  • Tested again, does not work
    – user168547
    Aug 14, 2022 at 11:51
  • Still works for me. I assume you don't see any errors, you just don't see the plugins deactivate? You're definitely installing it as a mu-plugin, and you've got the exact plugin names from the options table? You've got leading /s in your code but I don't see them in the options value. If that's not it then I guess you could try debugging it at your end e.g. add error_logs() to trace the values at the start and end and which if branches you're taking.
    – Rup
    Aug 14, 2022 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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