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:
- authenticate with admin credentials via PHP
- download a specific plugin from the repository
- install the
plugin - 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
1 Answer
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 plugin <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.