7

I've been working on a script that automatically downloads, installs and activates WordPress plugins. I wanted to share it with fellow users. Here's the solution I came up with:

HTML:

<input type="checkbox" name="plugins[]" value="backupwordpress|backupwordpress" checked="checked" /> Backupwordpress<br>
<input type="checkbox" name="plugins[]" value="google-analytics-dashboard-for-wp|gadwp" checked="checked" /> Google Analytics Dashboard for WP<br>

PHP:

if(isset($_POST['plugins'])){
  if (is_array($_POST['plugins'])) {
    foreach($_POST['plugins'] as $plugin){

        $plugin = explode('|', $plugin);

        echo "plugin value 1: " . $plugin[0];
        echo "plugin value 2: " . $plugin[1];

        echo $plugin[0] . ' selected' . '<br>';
        $plugin_path = 'https://downloads.wordpress.org/plugin/' . $plugin[0] . '.latest-stable.zip';
        file_put_contents($plugin[0] . '.zip', file_get_contents($plugin_path));

        print($plugin[0] . ' downloaded!' . '<br>');

        $theme_extraction_directory = ('temp/' . $dirname . '/wp-content/plugins/');

        $zip = new ZipArchive;
        $res = $zip->open($plugin[0] . '.zip');
        if ($res === TRUE) {

          $zip->extractTo($theme_extraction_directory);
          $zip->close();

          print($plugin . ' unzipped!' . '<br><br>');

          $active_plugins_count++;
          $plugin_number++;
          $plugin_file_path = $plugin[0] . '/' . $plugin[1] . '.php';
          $string_char_count = strlen($plugin_file_path);
          $active_plugins_beginning_part = 'a:' . $active_plugins_count . ':{';
          $active_plugins_end_part = '}';
          $active_plugins_string .= 'i:' . $plugin_number . ';s:' . $string_char_count . ':"' . $plugin_file_path . '";';

          // DELETE THE PLUGIN ZIP FILE
          unlink($plugin[0] . '.zip');

          print($plugin[0] . '.zip file DELETED!' . '<br><br>');

        } else {

          print($plugin[0] . ' zip file NOT found!' . '<br><br>');

        }

    }
  } else {

        $plugin_path = 'https://downloads.wordpress.org/plugin/' . $plugin[0] . '.latest-stable.zip';
        file_put_contents($plugin[0] . '.zip', file_get_contents($plugin_path));

        print($plugin[0] . ' downloaded!' . '<br>');

  }

  $full_active_plugin_string = $active_plugins_beginning_part . $active_plugins_string . $active_plugins_end_part;

  print('Full plugin string: ' . $full_active_plugin_string . '<br><br>');

  $no_active_plugins_string = 'a:0:{}';

  $sql = "UPDATE wp_options SET option_value = '$full_active_plugin_string' WHERE option_name = 'active_plugins'";

    if (mysql_query($sql, $link)) {
        print("Active Plugins field " . $full_active_plugin_string .  " updated successfully" . "<br><br>");
    } else {
        print('Error updating the active_plugins field: ' . mysql_error() . "<br><br>");
    }

}

I recently discovered that the latest version of a WordPress plugin can be found at:

https://downloads.wordpress.org/plugin/{plugin-name}.latest-stable.zip

The script works great but I had to include two values for each plugin checkbox, one representing the {plugin-name} and the other representing the filename that declares it a plugin in WordPress. It is disappointing that WordPress does not standardize this filename to something like plugin.php. That is why I had to add the second value to each plugin checkbox.

Anyway, this script works well but you do have to initially research the plugin name and plugin filename.. but I wanted to share it.

Which site can I post something that I want to share? I don't really have a question here.

  • 1
    You could put it up on GitHub as a repo or a gist – cameronjonesweb Jun 1 '16 at 7:07
6

Split this post into two parts: a question and an answer. Describe the problem you are trying to solve in the question. Write your solution as an answer.

This is explained in our help page: Can I answer my own question?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .