How I can Activate and deactivate plugin automatically? For example, I want the "Yoast Seo" plugin to be active only on Monday, Wednesday, Friday, Saturday and the other days deactivated. How can I do it? Thanks..

2 Answers 2


I like the idea of running this through a schedule event in WordPress, https://codex.wordpress.org/Function_Reference/wp_schedule_event

Here is a snippet that hopefully will get you there:

    wp_schedule_event( time(), 'daily', 'daily_plugin_check' );

  add_action( 'daily_plugin_check', 'toggle_plugins' );

  function toggle_plugins() {
      case 'Mon' :
      case 'Wed' :
      case 'Fri' :
        // Could be an array or a single string
        $plugin_to_activate = 'akismet/akismet.php';
        $plugin_to_deactivate = 'akismet/akismet.php';
      // Continue with each day you'd like to activate / deactive them

      require_once ABSPATH . 'wp-admin/includes/plugin.php';

      // If $plugin_to_activate is an array, then you can foreach of it

      // If $plugin_to_activate is an array, then you can foreach of it


Hope that helps!

  • 1
    Nice answer. One caveat is that this relies on CRON, which can be shut down depending on the environment being used. I've banged my head against that one before! Commented Jun 24, 2019 at 21:04
  • @christopherjones looks very promising. I added your code to wp-cron.php but i need a little help how and where to modify it. For instance I need to add days and hours; Mondays GMT +1 9 pm till 6 am ACTIVATED Tuesdays GMT +1 6:01 am till 8:59 DEACTIVATED Any help is welcome! Rob Commented Dec 17, 2020 at 12:19
  • 1
    @user9088656 you should not modify WP Core files, wp-admin wp-includes and the files in the root of the WP folder should never be modified ( with the exception of wp-config.php ). Otherwise, if you have a new question, you should create a new question, don't post it as a solution to somebody elses question
    – Tom J Nowell
    Commented Dec 17, 2020 at 16:10

You can mange this by adding the following to your functions.php file.

Checking todays timestamp for the days you want the plugin active - on the off days the plugin is disabled.

$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' );

It uses activate_plugin() to activate and deactivate_plugins() to deactivate the plugin.

https://developer.wordpress.org/reference/functions/activate_plugin/ https://developer.wordpress.org/reference/functions/deactivate_plugins/

  • 1
    This is nice and clean, but 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. Commented Jun 24, 2019 at 20:51
  • @NathanPowell Good point! Commented Jun 24, 2019 at 20:53

Your Answer

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

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