Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save infinityonlinesolutions/40de9cade13d90cb38b94cae768375d7 to your computer and use it in GitHub Desktop.
Save infinityonlinesolutions/40de9cade13d90cb38b94cae768375d7 to your computer and use it in GitHub Desktop.

Revisions

  1. @hansschuijff hansschuijff revised this gist Jan 6, 2021. 1 changed file with 78 additions and 45 deletions.
    123 changes: 78 additions & 45 deletions install-wp-plugins.php
    Original file line number Diff line number Diff line change
    @@ -26,21 +26,6 @@
    require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );

    /**
    * Gets runtime config data for a given context.
    *
    * @param string $context What config data needs to be returned?
    * @return array Runtime config for that context.
    */
    function get_config( $context ) {
    if ( 'must-have-plugins' === $context ) {
    // Array of plugin basenames of plugins that need to be active.
    return $plugins = array(
    'contact-form-7/wp-contact-form-7.php',
    );
    }
    }

    /*
    * Hide the 'Activate Plugin' and other links when not using Quiet_Upgrader_Skin as these links will
    * fail when not called from /wp-admin
    @@ -70,6 +55,7 @@ public function feedback( $string, ...$args ) {
    * @return void
    */
    function activate_plugin( $plugin ) {
    $plugin_mainfile = trailingslashit( WP_PLUGIN_DIR ) . $plugin;
    /* Nothing to do, when plugin already active.
    *
    * WARNING: When a plugin has been removed by ftp,
    @@ -78,22 +64,48 @@ function activate_plugin( $plugin ) {
    * (and it checks the existence of it).
    */
    if ( \is_plugin_active( $plugin ) ) {
    return;
    // Make sure the plugin is still there (files could be removed without wordpress noticing)
    $error = \validate_plugin( $plugin );
    if ( ! is_wp_error( $error ) ) {
    return;
    }
    }

    // Install if neccessary.
    if ( ! is_plugin_installed( $plugin ) ) {
    install_plugin( $plugin );
    $error = install_plugin( $plugin );
    if ( ! empty( $error ) ) {
    return $error;
    }
    }
    // Now we activate, when install has been successfull.
    if ( is_plugin_installed( $plugin ) ) {
    $pluginPath = trailingslashit( WP_PLUGIN_DIR ) . $plugin;
    if ( file_exists( $pluginPath ) ) {
    \activate_plugin( $pluginPath );
    } else {
    echo 'Error: Plugin file not activated (' . $plugin . '). This probably means the main '
    . 'file\'s name does not match the slug. Check the plugins listing in wp-admin.<br>'
    . "\n";
    }
    if ( ! is_plugin_installed( $plugin ) ) {
    return 'Error: Plugin could not be installed (' . $plugin . '). '
    . '<br>This probably means there is an error in the plugin basename, '
    . 'or the plugin isn\'t in the wordpress repository on wordpress.org. '
    . '<br>Please correct the problem, and/or install and activate the plugin manually.<br>'
    . "\n";
    }

    $error = \validate_plugin( $plugin );
    if ( is_wp_error( $error ) ) {
    return 'Error: Plugin main file has not been found (' . $plugin . ').'
    . '<br/>This probably means the main file\'s name does not match the slug.'
    . '<br/>Please check the plugins listing in wp-admin.'
    . "<br>\n"
    . var_export( $error->get_error_code(), true ) . ': '
    . var_export( $error->get_error_message(), true )
    . "\n";
    }
    $error = \activate_plugin( $plugin_mainfile );
    if ( is_wp_error( $error ) ) {
    return 'Error: Plugin has not been activated (' . $plugin . ').'
    . '<br/>This probably means the main file\'s name does not match the slug.'
    . '<br/>Check the plugins listing in wp-admin.'
    . "<br/>\n"
    . var_export( $error->get_error_code(), true ) . ': '
    . var_export( $error->get_error_message(), true )
    . "\n";
    }
    }

    @@ -103,7 +115,7 @@ function activate_plugin( $plugin ) {
    * Get_plugins() returns an array containing all installed plugins
    * with the plugin basename as key.
    *
    * When you pass the plugin basedir to get_plugins(),
    * When you pass the plugin dir to get_plugins(),
    * it will return an empty array if that plugin is not yet installed,
    *
    * When the plugin is installed it will return an array with that plugins data,
    @@ -113,7 +125,7 @@ function activate_plugin( $plugin ) {
    * @return boolean True when installed, otherwise false.
    */
    function is_plugin_installed( $plugin ) {
    $plugins = \get_plugins( '/'.get_plugin_basedir( $plugin ) );
    $plugins = \get_plugins( '/'.get_plugin_dir( $plugin ) );
    if ( ! empty( $plugins ) ) {
    return true;
    }
    @@ -126,27 +138,27 @@ function is_plugin_installed( $plugin ) {
    * @param string $plugin Plugin basename.
    * @return string The directory-part of the plugin basename.
    */
    function get_plugin_basedir( $plugin ) {
    function get_plugin_dir( $plugin ) {
    $chunks = explode( '/', $plugin );
    if ( ! is_array( $chunks ) ) {
    $plugin_basedir = $chunks;
    $plugin_dir = $chunks;
    } else{
    $plugin_basedir = $chunks[0];
    $plugin_dir = $chunks[0];
    }
    return $plugin_basedir;
    return $plugin_dir;
    }

    /**
    * Intall a given plugin.
    *
    * @param string $plugin Plugin basename.
    * @return bool True when install was succesfull, otherwise false.
    * @param string $plugin Plugin basename.
    * @return null|string Null when install was succesfull, otherwise error message.
    */
    function install_plugin( $plugin ) {
    $api = plugins_api(
    'plugin_information',
    array(
    'slug' => get_plugin_basedir( $plugin ),
    'slug' => get_plugin_dir( $plugin ),
    'fields' => array(
    'short_description' => false,
    'requires' => false,
    @@ -167,16 +179,34 @@ function install_plugin( $plugin ) {
    // Replace new \Plugin_Installer_Skin with new Quiet_Upgrader_Skin when output needs to be suppressed.
    $skin = new \Plugin_Installer_Skin( array( 'api' => $api ) );
    $upgrader = new \Plugin_Upgrader( $skin );
    $installed = $upgrader->install( $api->download_link );

    // check for errors
    if ( true !== $installed ) {
    echo 'Error: Install process failed (' . $plugin . '). var_dump of result follows.<br>'
    $error = $upgrader->install( $api->download_link );
    /*
    * Check for errors...
    * $upgrader->install() returns NULL on success,
    * otherwise a WP_Error object.
    */
    if ( is_wp_error( $error ) ) {
    return 'Error: Install process failed (' . $plugin . ').<br>'
    . "\n"
    . var_export( $error->get_error_code(), true ) . ': '
    . var_export( $error->get_error_message(), true )
    . "\n";
    var_dump( $installed ); // can be 'null' or WP_Error
    return false;
    }
    return true;
    }

    /**
    * Gets runtime config data for a given context.
    *
    * @param string $context What config data needs to be returned?
    * @return array Runtime config for that context.
    */
    function get_config( $context ) {
    if ( 'must-have-plugins' === $context ) {
    // Array of plugin basenames of plugins that need to be active.
    return $plugins = array(
    'contact-form-7/wp-contact-form-7.php',
    );
    }
    }

    /**
    @@ -186,8 +216,11 @@ function install_plugin( $plugin ) {
    */
    function activate_required_plugins() {
    $plugins = get_config('must-have-plugins');
    foreach ($plugins as $plugin) {
    activate_plugin( $plugin );
    foreach ( $plugins as $plugin ) {
    $error = activate_plugin( $plugin );
    if ( ! empty( $error ) ) {
    echo $error;
    }
    }
    }
    activate_required_plugins();
  2. @hansschuijff hansschuijff revised this gist Jan 6, 2021. 1 changed file with 25 additions and 27 deletions.
    52 changes: 25 additions & 27 deletions install-wp-plugins.php
    Original file line number Diff line number Diff line change
    @@ -1,37 +1,31 @@
    <?php
    /**
    * Plugin Name: Programmatically install and activate plugins
    * Description: Set Production URL for BE Media from Production
    * Plugin Name: Activate required plugins.
    * Description: Programmatically install and activate plugins based on a runtime config.
    * Version: 1.0
    * Author: Hans Schuijff
    * Author URI: http://dewitteprins.nl
    * License: MIT
    * License URI: http://www.opensource.org/licenses/mit-license.php
    */
    /**

    /**
    * Inspired by https://gist.github.com/squarestar/37fe1ff964adaddc0697dd03155cf0d0
    *
    * Programmatically install and activate wordpress plugins
    *
    * Usage:
    * 1. Edit the $plugin_slugs array at the beginning of this file to include the slugs of all the
    * plugins you want to install and activate
    * 2. Upload this file to the wordpress root directory (the same directory that contains the
    * 'wp-admin' directory).
    * 3. Navigate to <your-domain-wordpress-root>/install-wp-plugins.php (If wordpress is installed
    * in its own sub-directory, <your-domain-wordpress-root> is that sub-directory, not the root
    * domain of the site, e.g., example.com/wordpress/install-wp-plugins.php)
    * TODO:
    * - can the deactivation action links be removed or be caught with a better response?
    * - use core functionality runtime config.
    */
    namespace DeWittePrins\CoreFunctionality\MustUse;
    require_once( ABSPATH . '/wp-load.php' );

    require_once( ABSPATH . 'wp-load.php' );
    require_once( ABSPATH . 'wp-includes/pluggable.php');
    require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/misc.php' );
    require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );


    /**
    * Gets runtime config data for a given context.
    *
    @@ -40,11 +34,9 @@
    */
    function get_config( $context ) {
    if ( 'must-have-plugins' === $context ) {
    // Returns array of plugin basenames of plugins that need to be active.
    // Array of plugin basenames of plugins that need to be active.
    return $plugins = array(
    'contact-form-7/wp-contact-form-7.php',
    // 'simple-cache/simple-cache.php',
    // 'codepress-admin-columns/codepress-admin-columns.php',
    );
    }
    }
    @@ -53,7 +45,7 @@ function get_config( $context ) {
    * Hide the 'Activate Plugin' and other links when not using Quiet_Upgrader_Skin as these links will
    * fail when not called from /wp-admin
    */
    // This is to general to be used.
    // This will remove all links, so it is to generic to be usable. Have to replace it with a better solution.
    // echo '<style>a {display: none;}</style>';

    /**
    @@ -94,7 +86,7 @@ function activate_plugin( $plugin ) {
    }
    // Now we activate, when install has been successfull.
    if ( is_plugin_installed( $plugin ) ) {
    $pluginPath = WP_PLUGIN_DIR . '/' . $plugin;
    $pluginPath = trailingslashit( WP_PLUGIN_DIR ) . $plugin;
    if ( file_exists( $pluginPath ) ) {
    \activate_plugin( $pluginPath );
    } else {
    @@ -111,12 +103,18 @@ function activate_plugin( $plugin ) {
    * Get_plugins() returns an array containing all installed plugins
    * with the plugin basename as key.
    *
    * When you pass the plugin basedir to get_plugins(),
    * it will return an empty array if that plugin is not yet installed,
    *
    * When the plugin is installed it will return an array with that plugins data,
    * using the plugins main filename as key (so not the basename).
    *
    * @param string $plugin Plugin basename.
    * @return boolean True when installed, otherwise false.
    */
    function is_plugin_installed( $plugin ) {
    $plugins = \get_plugins( get_plugin_base_dir( $plugin ) );
    if ( isset( $plugins[ $plugin ] ) ) {
    $plugins = \get_plugins( '/'.get_plugin_basedir( $plugin ) );
    if ( ! empty( $plugins ) ) {
    return true;
    }
    return false;
    @@ -128,14 +126,14 @@ function is_plugin_installed( $plugin ) {
    * @param string $plugin Plugin basename.
    * @return string The directory-part of the plugin basename.
    */
    function get_plugin_base_dir( $plugin ) {
    function get_plugin_basedir( $plugin ) {
    $chunks = explode( '/', $plugin );
    if ( ! is_array( $chunks ) ) {
    $plugin_base_dir = $chunks;
    $plugin_basedir = $chunks;
    } else{
    $plugin_base_dir = $chunks[0];
    $plugin_basedir = $chunks[0];
    }
    return $plugin_base_dir;
    return $plugin_basedir;
    }

    /**
    @@ -148,7 +146,7 @@ function install_plugin( $plugin ) {
    $api = plugins_api(
    'plugin_information',
    array(
    'slug' => get_plugin_base_dir( $plugin ),
    'slug' => get_plugin_basedir( $plugin ),
    'fields' => array(
    'short_description' => false,
    'requires' => false,
  3. @hansschuijff hansschuijff revised this gist Jan 6, 2021. No changes.
  4. @hansschuijff hansschuijff revised this gist Jan 6, 2021. 1 changed file with 118 additions and 39 deletions.
    157 changes: 118 additions & 39 deletions install-wp-plugins.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,16 @@
    <?php
    /**
    * Plugin Name: Programmatically install and activate plugins
    * Description: Set Production URL for BE Media from Production
    * Version: 1.0
    * Author: Hans Schuijff
    * Author URI: http://dewitteprins.nl
    * License: MIT
    * License URI: http://www.opensource.org/licenses/mit-license.php
    */
    /**
    * Inspired by https://gist.github.com/squarestar/37fe1ff964adaddc0697dd03155cf0d0
    *
    * Programmatically install and activate wordpress plugins
    *
    * Usage:
    @@ -11,26 +22,40 @@
    * in its own sub-directory, <your-domain-wordpress-root> is that sub-directory, not the root
    * domain of the site, e.g., example.com/wordpress/install-wp-plugins.php)
    */
    namespace DeWittePrins\CoreFunctionality;

    $plugin_slugs = array(
    'contact-form-7/contact-form-7.php',
    'simple-cache/simple-cache.php',
    'codepress-admin-columns/codepress-admin-columns.php',
    );

    require_once( dirname( __FILE__ ) . '/wp-load.php' );
    namespace DeWittePrins\CoreFunctionality\MustUse;
    require_once( ABSPATH . '/wp-load.php' );
    require_once( ABSPATH . 'wp-includes/pluggable.php');
    require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/misc.php' );
    require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );


    /**
    * Gets runtime config data for a given context.
    *
    * @param string $context What config data needs to be returned?
    * @return array Runtime config for that context.
    */
    function get_config( $context ) {
    if ( 'must-have-plugins' === $context ) {
    // Returns array of plugin basenames of plugins that need to be active.
    return $plugins = array(
    'contact-form-7/wp-contact-form-7.php',
    // 'simple-cache/simple-cache.php',
    // 'codepress-admin-columns/codepress-admin-columns.php',
    );
    }
    }

    /*
    * Hide the 'Activate Plugin' and other links when not using Quiet_Upgrader_Skin as these links will
    * fail when not called from /wp-admin
    */
    echo '<style>a {display: none;}</style>';
    // This is to general to be used.
    // echo '<style>a {display: none;}</style>';

    /**
    * Overwrite the feedback method in the WP_Upgrader_Skin
    * to suppress the normal feedback.
    @@ -39,50 +64,91 @@ class Quiet_Upgrader_Skin extends \WP_Upgrader_Skin {
    /*
    * Suppress normal upgrader feedback / output
    */
    public function feedback( $string ) {
    public function feedback( $string, ...$args ) {
    /* no output */
    }
    }

    /**
    * Download, install and activate a plugin
    *
    * If the plugin directory already exists, this will only try to activate the plugin
    * Activates a given plugin.
    *
    * @param string $slug The slug of the plugin (should be the same as the plugin's directory name
    * If needed it dowloads and/or installs the plugin first.
    *
    * @param string $slug The plugin's basename (containing the plugin's base directory and the bootstrap filename).
    * @return void
    */
    function activate_plugin( $plugin ) {
    // nothing to do if plugin already active
    if ( \is_plugin_active( $plugin ) ) {
    /* Nothing to do, when plugin already active.
    *
    * WARNING: When a plugin has been removed by ftp,
    * WordPress will still consider it active,
    * untill the plugin list has been visited
    * (and it checks the existence of it).
    */
    if ( \is_plugin_active( $plugin ) ) {
    return;
    }
    // install plugin if not yet installed
    // Install if neccessary.
    if ( ! is_plugin_installed( $plugin ) ) {
    install_plugin( $plugin );
    }
    /*
    * The install results don't indicate what the main plugin file is, so we just try to
    * activate based on the slug. It may fail, in which case the plugin will have to be activated
    * manually from the admin screen.
    */
    $pluginPath = $plugin_dir . '/' . $plugin;
    if ( file_exists( $pluginPath ) ) {
    \activate_plugin( $pluginPath );
    } else {
    echo 'Error: Plugin file not activated (' . $plugin . '). This probably means the main '
    . 'file\'s name does not match the slug. Check the plugins listing in wp-admin.<br>'
    . "\n";
    // Now we activate, when install has been successfull.
    if ( is_plugin_installed( $plugin ) ) {
    $pluginPath = WP_PLUGIN_DIR . '/' . $plugin;
    if ( file_exists( $pluginPath ) ) {
    \activate_plugin( $pluginPath );
    } else {
    echo 'Error: Plugin file not activated (' . $plugin . '). This probably means the main '
    . 'file\'s name does not match the slug. Check the plugins listing in wp-admin.<br>'
    . "\n";
    }
    }
    }

    /*
    * Don't install plugins that already installed
    */
    /**
    * Is plugin installed?
    *
    * Get_plugins() returns an array containing all installed plugins
    * with the plugin basename as key.
    *
    * @param string $plugin Plugin basename.
    * @return boolean True when installed, otherwise false.
    */
    function is_plugin_installed( $plugin ) {
    $plugins = \get_plugins( get_plugin_base_dir( $plugin ) );
    if ( isset( $plugins[ $plugin ] ) ) {
    return true;
    }
    return false;
    }

    /**
    * Extraxts the plugins directory (=slug for api) from the plugin basename.
    *
    * @param string $plugin Plugin basename.
    * @return string The directory-part of the plugin basename.
    */
    function get_plugin_base_dir( $plugin ) {
    $chunks = explode( '/', $plugin );
    if ( ! is_array( $chunks ) ) {
    $plugin_base_dir = $chunks;
    } else{
    $plugin_base_dir = $chunks[0];
    }
    return $plugin_base_dir;
    }

    /**
    * Intall a given plugin.
    *
    * @param string $plugin Plugin basename.
    * @return bool True when install was succesfull, otherwise false.
    */
    function install_plugin( $plugin ) {
    $api = plugins_api(
    'plugin_information',
    array(
    'slug' => $plugin,
    'slug' => get_plugin_base_dir( $plugin ),
    'fields' => array(
    'short_description' => false,
    'requires' => false,
    @@ -99,18 +165,31 @@ function install_plugin( $plugin ) {
    ),
    )
    );
    // Replace with new Quiet_Upgrader_Skin for no output
    $skin = new Plugin_Installer_Skin( array( 'api' => $api ) );
    $upgrader = new Plugin_Upgrader( $skin );

    // Replace new \Plugin_Installer_Skin with new Quiet_Upgrader_Skin when output needs to be suppressed.
    $skin = new \Plugin_Installer_Skin( array( 'api' => $api ) );
    $upgrader = new \Plugin_Upgrader( $skin );
    $installed = $upgrader->install( $api->download_link );

    // check for errors
    if ( true !== $installed ) {
    echo 'Error: Install process failed (' . $plugin . '). var_dump of result follows.<br>'
    . "\n";
    var_dump( $installed ); // can be 'null' or WP_Error
    return false;
    }
    return true;
    }

    foreach ($plugins as $plugin) {
    activate_plugin( $plugin );
    /**
    * Launches auto-activation of required plugins.
    *
    * @return void
    */
    function activate_required_plugins() {
    $plugins = get_config('must-have-plugins');
    foreach ($plugins as $plugin) {
    activate_plugin( $plugin );
    }
    }
    activate_required_plugins();
  5. @hansschuijff hansschuijff revised this gist Jan 5, 2021. No changes.
  6. @hansschuijff hansschuijff revised this gist Jan 5, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions install-wp-plugins.php
    Original file line number Diff line number Diff line change
    @@ -111,6 +111,6 @@ function install_plugin( $plugin ) {
    }
    }

    foreach ($plugin_slugs as $pluginSlug) {
    activate_plugin( $pluginSlug );
    foreach ($plugins as $plugin) {
    activate_plugin( $plugin );
    }
  7. @hansschuijff hansschuijff revised this gist Jan 5, 2021. 1 changed file with 71 additions and 61 deletions.
    132 changes: 71 additions & 61 deletions install-wp-plugins.php
    Original file line number Diff line number Diff line change
    @@ -11,29 +11,37 @@
    * in its own sub-directory, <your-domain-wordpress-root> is that sub-directory, not the root
    * domain of the site, e.g., example.com/wordpress/install-wp-plugins.php)
    */
    namespace DeWittePrins\CoreFunctionality;

    $plugin_slugs = array(
    'contact-form-7',
    'simple-cache',
    'codepress-admin-columns',
    'contact-form-7/contact-form-7.php',
    'simple-cache/simple-cache.php',
    'codepress-admin-columns/codepress-admin-columns.php',
    );

    require_once(dirname(__FILE__) . '/wp-load.php');
    require_once(ABSPATH . 'wp-admin/includes/plugin-install.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/misc.php');
    require_once(ABSPATH . 'wp-admin/includes/plugin.php');
    require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
    require_once( dirname( __FILE__ ) . '/wp-load.php' );
    require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/misc.php' );
    require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );

    /*
    * Hide the 'Activate Plugin' and other links when not using QuietSkin as these links will
    * Hide the 'Activate Plugin' and other links when not using Quiet_Upgrader_Skin as these links will
    * fail when not called from /wp-admin
    */
    echo '<style>a {display: none;}</style>';

    class QuietSkin extends \WP_Upgrader_Skin
    {
    public function feedback($string) { /* no output */ }
    /**
    * Overwrite the feedback method in the WP_Upgrader_Skin
    * to suppress the normal feedback.
    */
    class Quiet_Upgrader_Skin extends \WP_Upgrader_Skin {
    /*
    * Suppress normal upgrader feedback / output
    */
    public function feedback( $string ) {
    /* no output */
    }
    }

    /**
    @@ -43,64 +51,66 @@ public function feedback($string) { /* no output */ }
    *
    * @param string $slug The slug of the plugin (should be the same as the plugin's directory name
    */
    function require_plugin( $slug )
    {
    $pluginDir = WP_PLUGIN_DIR . '/' . $slug;
    /*
    * Don't try installing plugins that already exist (wastes time downloading files that
    * won't be used
    */
    if (!is_dir($pluginDir)) {
    $api = plugins_api(
    'plugin_information',
    array(
    'slug' => $slug,
    'fields' => array(
    'short_description' => false,
    'requires' => false,
    'sections' => false,
    'rating' => false,
    'ratings' => false,
    'downloaded' => false,
    'last_updated' => false,
    'added' => false,
    'tags' => false,
    'compatibility' => false,
    'homepage' => false,
    'donate_link' => false,
    ),
    )
    );

    // Replace with new QuietSkin for no output
    $skin = new Plugin_Installer_Skin(array('api' => $api));

    $upgrader = new Plugin_Upgrader($skin);

    $install = $upgrader->install($api->download_link);

    if ($install !== true) {
    echo 'Error: Install process failed (' . $slug . '). var_dump of result follows.<br>'
    . "\n";
    var_dump($install); // can be 'null' or WP_Error
    }
    function activate_plugin( $plugin ) {
    // nothing to do if plugin already active
    if ( \is_plugin_active( $plugin ) ) {
    return;
    }
    // install plugin if not yet installed
    if ( ! is_plugin_installed( $plugin ) ) {
    install_plugin( $plugin );
    }

    /*
    * The install results don't indicate what the main plugin file is, so we just try to
    * activate based on the slug. It may fail, in which case the plugin will have to be activated
    * manually from the admin screen.
    */
    $pluginPath = $pluginDir . '/' . $slug . '.php';
    if (file_exists($pluginPath)) {
    activate_plugin($pluginPath);
    $pluginPath = $plugin_dir . '/' . $plugin;
    if ( file_exists( $pluginPath ) ) {
    \activate_plugin( $pluginPath );
    } else {
    echo 'Error: Plugin file not activated (' . $slug . '). This probably means the main '
    echo 'Error: Plugin file not activated (' . $plugin . '). This probably means the main '
    . 'file\'s name does not match the slug. Check the plugins listing in wp-admin.<br>'
    . "\n";
    }
    }

    /*
    * Don't install plugins that already installed
    */
    function install_plugin( $plugin ) {
    $api = plugins_api(
    'plugin_information',
    array(
    'slug' => $plugin,
    'fields' => array(
    'short_description' => false,
    'requires' => false,
    'sections' => false,
    'rating' => false,
    'ratings' => false,
    'downloaded' => false,
    'last_updated' => false,
    'added' => false,
    'tags' => false,
    'compatibility' => false,
    'homepage' => false,
    'donate_link' => false,
    ),
    )
    );
    // Replace with new Quiet_Upgrader_Skin for no output
    $skin = new Plugin_Installer_Skin( array( 'api' => $api ) );
    $upgrader = new Plugin_Upgrader( $skin );
    $installed = $upgrader->install( $api->download_link );
    // check for errors
    if ( true !== $installed ) {
    echo 'Error: Install process failed (' . $plugin . '). var_dump of result follows.<br>'
    . "\n";
    var_dump( $installed ); // can be 'null' or WP_Error
    }
    }

    foreach ($plugin_slugs as $pluginSlug) {
    require_plugin( $pluginSlug );
    activate_plugin( $pluginSlug );
    }
  8. @hansschuijff hansschuijff revised this gist Jan 5, 2021. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions install-wp-plugins.php
    Original file line number Diff line number Diff line change
    @@ -57,17 +57,17 @@ function require_plugin( $slug )
    'slug' => $slug,
    'fields' => array(
    'short_description' => false,
    'sections' => false,
    'requires' => false,
    'rating' => false,
    'ratings' => false,
    'downloaded' => false,
    'last_updated' => false,
    'added' => false,
    'tags' => false,
    'compatibility' => false,
    'homepage' => false,
    'donate_link' => false,
    'requires' => false,
    'sections' => false,
    'rating' => false,
    'ratings' => false,
    'downloaded' => false,
    'last_updated' => false,
    'added' => false,
    'tags' => false,
    'compatibility' => false,
    'homepage' => false,
    'donate_link' => false,
    ),
    )
    );
  9. @hansschuijff hansschuijff revised this gist Jan 5, 2021. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions install-wp-plugins.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * Programmatically install and activate wordpress plugins
    *
    * Usage:
    * 1. Edit the $pluginSlugs array at the beginning of this file to include the slugs of all the
    * 1. Edit the $plugin_slugs array at the beginning of this file to include the slugs of all the
    * plugins you want to install and activate
    * 2. Upload this file to the wordpress root directory (the same directory that contains the
    * 'wp-admin' directory).
    @@ -12,7 +12,7 @@
    * domain of the site, e.g., example.com/wordpress/install-wp-plugins.php)
    */

    $pluginSlugs = array(
    $plugin_slugs = array(
    'contact-form-7',
    'simple-cache',
    'codepress-admin-columns',
    @@ -43,7 +43,7 @@ public function feedback($string) { /* no output */ }
    *
    * @param string $slug The slug of the plugin (should be the same as the plugin's directory name
    */
    function sswInstallActivatePlugin($slug)
    function require_plugin( $slug )
    {
    $pluginDir = WP_PLUGIN_DIR . '/' . $slug;
    /*
    @@ -101,6 +101,6 @@ function sswInstallActivatePlugin($slug)
    }
    }

    foreach ($pluginSlugs as $pluginSlug) {
    sswInstallActivatePlugin($pluginSlug);
    foreach ($plugin_slugs as $pluginSlug) {
    require_plugin( $pluginSlug );
    }
  10. @squarestar squarestar revised this gist Apr 27, 2017. 1 changed file with 11 additions and 12 deletions.
    23 changes: 11 additions & 12 deletions install-wp-plugins.php
    Original file line number Diff line number Diff line change
    @@ -3,16 +3,21 @@
    * Programmatically install and activate wordpress plugins
    *
    * Usage:
    * 1. Edit the $pluginSlugs array at the end of this file to include the slugs of all the plugins
    * you want to install and activate
    * 1. Edit the $pluginSlugs array at the beginning of this file to include the slugs of all the
    * plugins you want to install and activate
    * 2. Upload this file to the wordpress root directory (the same directory that contains the
    * 'wp-admin' directory).
    * 3. Navigate to <your-domain-wordpress-root>/install-wp-plugins.php
    *
    * If wordpress is installed in its own sub-directory, <your-domain-wordpress-root> is that
    * sub-directory, not the root domain of the site, e.g., example.com/wordpress
    * 3. Navigate to <your-domain-wordpress-root>/install-wp-plugins.php (If wordpress is installed
    * in its own sub-directory, <your-domain-wordpress-root> is that sub-directory, not the root
    * domain of the site, e.g., example.com/wordpress/install-wp-plugins.php)
    */

    $pluginSlugs = array(
    'contact-form-7',
    'simple-cache',
    'codepress-admin-columns',
    );

    require_once(dirname(__FILE__) . '/wp-load.php');
    require_once(ABSPATH . 'wp-admin/includes/plugin-install.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    @@ -96,12 +101,6 @@ function sswInstallActivatePlugin($slug)
    }
    }

    $pluginSlugs = array(
    'contact-form-7',
    'simple-cache',
    'codepress-admin-columns',
    );

    foreach ($pluginSlugs as $pluginSlug) {
    sswInstallActivatePlugin($pluginSlug);
    }
  11. @squarestar squarestar revised this gist Apr 27, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions install-wp-plugins.php
    Original file line number Diff line number Diff line change
    @@ -99,6 +99,7 @@ function sswInstallActivatePlugin($slug)
    $pluginSlugs = array(
    'contact-form-7',
    'simple-cache',
    'codepress-admin-columns',
    );

    foreach ($pluginSlugs as $pluginSlug) {
  12. @squarestar squarestar created this gist Mar 11, 2017.
    106 changes: 106 additions & 0 deletions install-wp-plugins.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    <?php
    /**
    * Programmatically install and activate wordpress plugins
    *
    * Usage:
    * 1. Edit the $pluginSlugs array at the end of this file to include the slugs of all the plugins
    * you want to install and activate
    * 2. Upload this file to the wordpress root directory (the same directory that contains the
    * 'wp-admin' directory).
    * 3. Navigate to <your-domain-wordpress-root>/install-wp-plugins.php
    *
    * If wordpress is installed in its own sub-directory, <your-domain-wordpress-root> is that
    * sub-directory, not the root domain of the site, e.g., example.com/wordpress
    */

    require_once(dirname(__FILE__) . '/wp-load.php');
    require_once(ABSPATH . 'wp-admin/includes/plugin-install.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/misc.php');
    require_once(ABSPATH . 'wp-admin/includes/plugin.php');
    require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');

    /*
    * Hide the 'Activate Plugin' and other links when not using QuietSkin as these links will
    * fail when not called from /wp-admin
    */
    echo '<style>a {display: none;}</style>';

    class QuietSkin extends \WP_Upgrader_Skin
    {
    public function feedback($string) { /* no output */ }
    }

    /**
    * Download, install and activate a plugin
    *
    * If the plugin directory already exists, this will only try to activate the plugin
    *
    * @param string $slug The slug of the plugin (should be the same as the plugin's directory name
    */
    function sswInstallActivatePlugin($slug)
    {
    $pluginDir = WP_PLUGIN_DIR . '/' . $slug;
    /*
    * Don't try installing plugins that already exist (wastes time downloading files that
    * won't be used
    */
    if (!is_dir($pluginDir)) {
    $api = plugins_api(
    'plugin_information',
    array(
    'slug' => $slug,
    'fields' => array(
    'short_description' => false,
    'sections' => false,
    'requires' => false,
    'rating' => false,
    'ratings' => false,
    'downloaded' => false,
    'last_updated' => false,
    'added' => false,
    'tags' => false,
    'compatibility' => false,
    'homepage' => false,
    'donate_link' => false,
    ),
    )
    );

    // Replace with new QuietSkin for no output
    $skin = new Plugin_Installer_Skin(array('api' => $api));

    $upgrader = new Plugin_Upgrader($skin);

    $install = $upgrader->install($api->download_link);

    if ($install !== true) {
    echo 'Error: Install process failed (' . $slug . '). var_dump of result follows.<br>'
    . "\n";
    var_dump($install); // can be 'null' or WP_Error
    }
    }

    /*
    * The install results don't indicate what the main plugin file is, so we just try to
    * activate based on the slug. It may fail, in which case the plugin will have to be activated
    * manually from the admin screen.
    */
    $pluginPath = $pluginDir . '/' . $slug . '.php';
    if (file_exists($pluginPath)) {
    activate_plugin($pluginPath);
    } else {
    echo 'Error: Plugin file not activated (' . $slug . '). This probably means the main '
    . 'file\'s name does not match the slug. Check the plugins listing in wp-admin.<br>'
    . "\n";
    }
    }

    $pluginSlugs = array(
    'contact-form-7',
    'simple-cache',
    );

    foreach ($pluginSlugs as $pluginSlug) {
    sswInstallActivatePlugin($pluginSlug);
    }