我喜欢通过WordPress中的日程安排活动来实现这一点,https://codex.wordpress.org/Function_Reference/wp_schedule_event
这里有一个片段,希望能帮助您达到目的:
<?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);
}
}
}
希望有帮助!