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


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!

| improve this answer | |
  • 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! – Nathan Powell Jun 24 '19 at 21:04

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/

| improve this answer | |
  • 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. – Nathan Powell Jun 24 '19 at 20:51
  • @NathanPowell Good point! – Liam Stewart Jun 24 '19 at 20:53

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.