1

I'm using a custom Php script to install WordPress via the Softaculous API. Once installed, what's the best way to programmatically install and activate a plugin? I could try setting up a custom WP script in Softaculous with a PHP script in a custom plugin in an mu-plugins folder, but I would prefer not to do that, and instead install/activate the plugin after the WP installation.

So what I would like to do after the WP main install and from an external PHP script if possible is:

  1. authenticate with admin credentials via PHP
  2. download a specific plugin from the repository
  3. install the plugin
  4. activate the plugin

Maybe I could use the TGM-Plugin-Activation to install/activate another plugin, but I would need to be able to install/activate that plugin itself. Is this possible without any admin user clicks and without using the mu-plugins folder pre-install?

I've read these posts:

Activating a single plugin via php

Plugin to install a plugin

5

WP CLI

The easy way; use the WP CLI (also available as wp-cli.phar) to get a solid, maintainable solution for this requirement. More for this topic you will find at the command


WP API

If you will need a custom script is is necessary to load the WP API, via wp-load.php and look for the function activate_plugin($path_to_the_plugin).

Activation

As example what you need to get all requirements to activate a plugin see below

define( 'WP_ADMIN', TRUE );
define( 'WP_NETWORK_ADMIN', TRUE ); // Need for Multisite
define( 'WP_USER_ADMIN', TRUE );

require_once('../wp-load.php');
require_once( '../wp-admin/includes/admin.php' );
require_once( '../wp-admin/includes/plugin.php' );

activate_plugin( 'PATH_TO_THE_PLUGIN' );

Installation

The installation of a plugin is also possible via the API of WP, also code thats should helps a little bit to run in the right direction.

// Include required libs for installation
require_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
require_once( ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php' );
require_once( ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php' );

// Get Plugin Info
$api = plugins_api( 'plugin_information',
    array(
        'slug' => $plugin,
        '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,
        ),
    )
);
$skin     = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );
$upgrader->install( $api->download_link );

A example that install and activate see you in this class.

| improve this answer | |
  • I don't want to use the command line, I want to run from a Php script. I will experiment with your code above and see if I can adapt the connekt class. Many thanks for this info! – Nick W Apr 26 '18 at 12:57
  • However you can also use the WP CLI possibilities via script, download the wp-cli.phar fro the WP CLI project and usage in your script. – bueltge Apr 26 '18 at 13:58
  • 2
    Thank you muchly for your updates. To use wp-cli from Php I believe would require using the shell_exec command which I've disabled on the server for security reasons. I'm going to test the API option ... – Nick W Apr 27 '18 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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