自动激活和停用插件

时间:2019-06-24 作者:Donovan

如何自动激活和停用插件?例如,我希望“Yoast Seo”插件仅在星期一、星期三、星期五、星期六和其他停用的日子处于活动状态。我该怎么做?谢谢

2 个回复
SO网友:ChristopherJones

我喜欢通过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);
      }
    }

  }

希望有帮助!

SO网友:Liam Stewart

您可以通过将以下内容添加到functions.php 文件

检查您希望插件处于活动状态的日期的todays时间戳-在禁用插件的休息日。

$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\' );
}
它使用activate_plugin() 激活和deactivate_plugins() 停用插件。

https://developer.wordpress.org/reference/functions/activate_plugin/https://developer.wordpress.org/reference/functions/deactivate_plugins/

相关推荐

Functions.php上未定义$_GET和&_REQUEST索引

我最近尝试学习为我的自定义主题创建一个主题选项页面,这是按照stackoverflow和其他资源的教程进行的。但脚本显示错误if ($_GET[\'page\'] == basename(__FILE__)) { if (\'save\' == $_REQUEST[\'formaction\']) { foreach ($options as $value) { if( isset( $_REQUEST[ $value[\'id\']