Claudiu Creanga
Claudiu Creanga

Reputation: 8366

code to activate plugin in wordpress

This client wants to automatically activate a wordpress plugin every Tuesday between some hours. This is because the plugin has conflicts with another plugin. I did not find anything on the net about that, how to do it... anyone knows what happens behind wordpress when the button activate plugin is clicked? I can't find that specific page in my wordpress folder... Thanks!

Something that I tried and not working:

require('/web/htdocs/www.fattorefamiglia.com/home/wp-content/plugins/quick-chat/quick-chat.php');

function toggle_plugin() {

    // Full path to WordPress from the root
    $wordpress_path = '/web/htdocs/www.fattorefamiglia.com/home/';

    // Absolute path to plugins dir
    $plugin_path = $wordpress_path.'wp-content/plugins/';

    // Absolute path to your specific plugin
    $my_plugin = $plugin_path.'quick-chat/quick-chat.php';
$start = strtotime('1:30');
$end = strtotime('22:30');
$timenow = date('U'); 

if((date('w') == 3) && ($timenow >= $start && $timenow <= $end)) { // day 2 = Tuesday
     activate_plugin($my_plugin);
    }
    else {
        deactivate_plugins($my_plugin);   
    }
} 

I put this code in functions.php

Upvotes: 2

Views: 4223

Answers (1)

Vivek Tamrakar
Vivek Tamrakar

Reputation: 522

Activate Plugin by Code in wordpress

  function run_activate_plugin( $plugin ) {
     $current = get_option( 'active_plugins' );
     $plugin = plugin_basename( trim( $plugin ) );

     if ( !in_array( $plugin, $current ) ) {
     $current[] = $plugin;
      sort( $current );
      do_action( 'activate_plugin', trim( $plugin ) );
      update_option( 'active_plugins', $current );
      do_action( 'activate_' . trim( $plugin ) );
      do_action( 'activated_plugin', trim( $plugin) );
     }

    return null;
  }
run_activate_plugin( 'plugin-folder-name/plugin-main-file.php' );

Upvotes: 2

Related Questions