Activate and Deactivate Plugin based on schedule

This is a code snippet for answering this question: https://www.facebook.com/groups/WP.org/permalink/10165475785265643/

You can use this code snippet to activate/ de-activate your plugin at specific time/day.

/**
 * Activate - Deactivate Plugin based on time
 * @author https://wordpress.stackexchange.com/questions/341339/activate-and-deactivate-plugin-automatically
 * @author https://wp-snippets.net/snippet/activate-and-deactivate-plugin-based-on-schedule
 */
$day = date( 'D'); // Get current Date (Mon - Sun)
$hour = date('H');//get current hour (00 - 24)

//Set schedule
$weekdays = ['Mon','Tue', 'Wed','Thu','Fri'];
//Time on weekdays: 9PM to 7AM
$weekdays_time = ['21','22','23','24','00','01','02','03','04','05','06','07'];
//for Weekend
$weekend = ['Sat','Sun'];
//Time on wekend: 5PM to 10AM
$weekend_time = ['17','18','19','20','21','22','23','24','00','01','02','03','04','05','06','07','8','9','10'];

//Activate or De-activate
if ( in_array( $day, $weekdays, true ) && in_array($hour, $weekdays_time, true) || in_array($day, $weekend, true) && in_array($hour, $weekend_time, true)) {
    //Yoast SEO plugin activated
    activate_plugin( '/wordpress-seo/wp-seo.php' );
} else { // Yoast SEO is deactivated
    deactivate_plugins( '/wordpress-seo/wp-seo.php' );
}

 

Have fun!

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments