Yes, it is possible to programmatically install plugins from a WordPress theme. This can be done using the Plugin_Upgrader class, which is part of the WordPress core. The process involves the following steps:

  1. Download the plugin: You can download the plugin using the wp_remote_get() function.
  2. Unpack the plugin: After downloading the plugin, you will need to unpack it using the unzip_file() function.
  3. Install the plugin: Once the plugin is unpacked, you can use the Plugin_Upgrader class to install it. This class will handle the installation process, including copying the plugin files to the appropriate location and activating the plugin.
  4. Error handling: It’s important to handle any errors that may occur during the installation process, such as a failure to download the plugin or a failure to unpack the plugin.

Here is an example of how you might programmatically install a plugin from a WordPress theme:


<?php

// Download the plugin
$response = wp_remote_get( 'https://example.com/my-plugin.zip' );

// Unpack the plugin
$plugin_zip = wp_upload_bits( 'my-plugin.zip', null, wp_remote_retrieve_body( $response ) );

// Install the plugin
$upgrader = new Plugin_Upgrader();
$result = $upgrader->install( $plugin_zip['file'] );

// Check for errors
if ( is_wp_error( $result ) ) {
    // Handle errors
} else {
    // Activate the plugin
    $result = activate_plugin( 'my-plugin/my-plugin.php' );
}
?>

Note- that installing plugins programmatically is not recommended for all situations, as it requires advanced knowledge of WordPress and can result in security or compatibility issues. It is recommended to use this method only in specific cases where it is necessary, such as during the installation of a theme or plugin bundle.