OK I know this has been asked before but I want to know the most efficient and clean way I can do this.

What I have is an auto provision through serverpilot which installs WordPress for a client on a digital ocean server.
The problem I'm having is this:
I want to activate a plugin that helps the client when he first starts out on WordPress. The problem I'm having is I can't have my script access their DB to activate the plugin once the script adds the file to the plugin directory. I want the plugin to be activated through PHP. So far this solution looks good but I want to know if this is the easiest and cleanest way of activating a plugin that I'm dropping into the plugins directory

function MY_toggle_plugins() {
    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    $temp_files1 = glob(WP_PLUGIN_DIR.'/*'); 
    foreach($temp_files1 as $file1){
        if(is_dir($file1)) { 
            $temp_files2 = glob($file1 . '/*');
            foreach($temp_files2 as $file2){
                if(is_file($file2) && stripos(file_get_contents($file2),'Plugin Name:')!==false) { 
                    if(is_plugin_active($plugin_name_full)) {
                        array_push($already_active, $plugin_name_full); 
                        array_push($activated, $plugin_name_full);

    echo 'You have activated these plugins:<br/><br/>'.serialize($activated).'<br/><br/>These were already active:<br/><br/>'.serialize($already_active); exit;

1 Answer 1


If you already have the plugin installed as part of the initial setup, you can add a loader for it in mu-plugins that activates it.

Must-use plugins don't require activation themselves, so adding a call to activate_plugin() inside it will attempt to automatically activate your plugin on every load. That becomes a problem if you ever want to deactivate the plugin since it'll just reactivate on the next page load, but one way around that is to remove the must-use plugin to enable deactivating the other one.

  • I don't have any other plugins on the website. As it is a brand new provisioned instance it's basically a clean install which is why I want activate another plugin which one of my scripts drops in the plugin directory. Activating itself on every load of the functions.php is fine I might build the plugin to remove that line from the functions.php once it is activated. Appreciate the suggestion if you have any other thoughts please post them. Thank you
    – Alex
    Jun 11, 2017 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.