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

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_acplugin_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!

  • 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
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/

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