0

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

1

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:

<?php
  
  if(!wp_next_scheduled('daily_plugin_check')){
    wp_schedule_event( time(), 'daily', 'daily_plugin_check' );
  }

  add_action( 'daily_plugin_check', 'toggle_plugins' );

  function toggle_plugins() {
    switch(date('D')){
      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';
        break;
      // Continue with each day you'd like to activate / deactive them
    }

    if(!function_exists('activate_plugin')){
      require_once ABSPATH . 'wp-admin/includes/plugin.php';
    }

    if(!empty($plugin_to_activate){
      // If $plugin_to_activate is an array, then you can foreach of it
      if(!is_plugin_active($plugin_to_activate)){
        activate_plugin($plugin_to_activate);
      }
    }

    if(!empty($plugin_to_deactivate){
      // If $plugin_to_activate is an array, then you can foreach of it
      if(is_plugin_active($plugin_to_deactivate)){
        deactivate_plugins($plugin_to_deactivate);
      }
    }

  }

Hope that helps!

3
  • 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
0

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/

2
  • 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.