Skip to content

Instantly share code, notes, and snippets.

@codersantosh
Last active July 17, 2024 15:24
Show Gist options
  • Select an option

  • Save codersantosh/f60fddea449846dc8344214c1938c924 to your computer and use it in GitHub Desktop.

Select an option

Save codersantosh/f60fddea449846dc8344214c1938c924 to your computer and use it in GitHub Desktop.
Programmatically install and activate WordPress plugins
/**
* Programmatically install and activate WordPress plugins.
* Supports installation of plugins from the WordPress plugin repository and external sources via ZIP files.
*/
if ( ! function_exists( 'prefix_is_plugin_active' ) ) {
/**
* Checks if a given plugin is active.
*
* @since 1.0.0
*
* @param string $plugin Plugin folder with main file e.g., my-plugin/my-plugin.php.
* @return bool True if the plugin is active, otherwise false.
*/
function prefix_is_plugin_active( $plugin ) {
return is_plugin_active_for_network( $plugin ) || is_plugin_active( $plugin );
}
}
if ( ! function_exists( 'prefix_install_plugin' ) ) {
/**
* Install and activate a WordPress plugin.
*
* @param array $plugin_info Plugin information array containing 'name', 'slug', 'plugin', and 'source'(optional).
* @return array Associative array with 'success' boolean and 'message' string.
*/
function prefix_install_plugin( $plugin_info ) {
$name = sanitize_text_field( $plugin_info['name'] );
$slug = sanitize_key( $plugin_info['slug'] );
$plugin = sanitize_text_field( $plugin_info['plugin'] );
$source = isset( $plugin_info['source'] ) ? esc_url_raw( $plugin_info['source'] ) : '';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
include_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( prefix_is_plugin_active( $plugin ) ) {
// Plugin is already active.
return array(
'success' => true,
'message' => sprintf(
/* translators: %s is the plugin name */
esc_html__( 'Plugin "%s" is already active.', 'text-domain' ),
$name
),
);
}
// The plugin is installed, but not active.
if ( file_exists( WP_PLUGIN_DIR . '/' . $slug ) ) {
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
if ( prefix_is_plugin_active( $plugin ) ) {
// Plugin is already active.
return array(
'success' => true,
'message' => sprintf(
/* translators: %s is the plugin name */
esc_html__( 'Plugin "%s" is already active.', 'text-domain' ),
$name
),
);
}
if ( current_user_can( 'activate_plugin', $plugin ) ) {
$result = activate_plugin( $plugin );
if ( is_wp_error( $result ) ) {
// Plugin is already active.
return array(
'success' => false,
'message' => sprintf(
/* translators: %1$s is the plugin name, %2$s is error message */
esc_html__( 'Error activating plugin "%1$s": %2$s', 'text-domain' ),
$name,
$result->get_error_message()
),
);
}
return array(
'success' => true,
'message' => sprintf(
/* translators: %s is the plugin name.*/
esc_html__( 'Plugin "%s" activated successfully.', 'text-domain' ),
$name,
),
);
} else {
return array(
'success' => false,
'message' => sprintf(
/* translators: %s is the plugin name.*/
esc_html__( 'You don\'t have permission to activate the plugin "%s".', 'text-domain' ),
$name,
),
);
}
}
if ( $source ) {
// Install plugin from external source.
$download_link = $source;
} else {
// Install plugin from WordPress repository.
$api = plugins_api(
'plugin_information',
array(
'slug' => $slug,
'fields' => array( 'sections' => false ),
)
);
if ( is_wp_error( $api ) ) {
return array(
'success' => false,
'message' => sprintf(
/* translators: %1$s is the plugin name, %2$s is error message */
esc_html__( 'Error retrieving information for plugin "%1$s": %2$s', 'text-domain' ),
$name,
$result->get_error_message()
),
);
}
$download_link = $api->download_link;
}
$skin = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );
$result = $upgrader->install( $download_link );
if ( is_wp_error( $result ) ) {
return array(
'success' => false,
'message' => sprintf(
/* translators: %1$s is the plugin name, %2$s is error message */
esc_html__( 'Error installing plugin "%1$s": %2$s', 'text-domain' ),
$name,
$result->get_error_message()
),
);
} elseif ( is_wp_error( $skin->result ) ) {
return array(
'success' => false,
'message' => sprintf(
/* translators: %1$s is the plugin name, %2$s is error message */
esc_html__( 'Error installing plugin "%1$s": %2$s', 'text-domain' ),
$name,
$skin->result->get_error_message()
),
);
} elseif ( $skin->get_errors()->get_error_code() ) {
return array(
'success' => false,
'message' => sprintf(
/* translators: %1$s is the plugin name, %2$s is error message */
esc_html__( 'Error installing plugin "%1$s": %2$s', 'text-domain' ),
$name,
$skin->get_error_messages()
),
);
} elseif ( is_null( $result ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();
global $wp_filesystem;
$error_message = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'text-domain' );
if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) {
$error_message = esc_html( $wp_filesystem->errors->get_error_message() );
}
return array(
'success' => false,
'message' => sprintf(
/* translators: %1$s is the plugin name, %2$s is error message */
esc_html__( 'Error installing plugin "%1$s": %2$s', 'text-domain' ),
$name,
$error_message
),
);
}
if ( prefix_is_plugin_active( $plugin ) ) {
// Plugin is already active.
return array(
'success' => true,
'message' => sprintf(
/* translators: %s is the plugin name.*/
esc_html__( 'Plugin "%s" activated successfully.', 'text-domain' ),
$name,
),
);
}
if ( current_user_can( 'activate_plugin', $plugin ) ) {
$result = activate_plugin( $plugin );
if ( is_wp_error( $result ) ) {
return array(
'success' => false,
'message' => sprintf(
/* translators: %1$s is the plugin name, %2$s is error message */
esc_html__( 'Error activating plugin "%1$s": %2$s', 'text-domain' ),
$name,
$result->get_error_message()
),
);
}
} else {
return array(
'success' => false,
'message' => sprintf(
/* translators: %s is the plugin name.*/
esc_html__( 'You don\'t have permission to activate the plugin "%s".', 'text-domain' ),
$name,
),
);
}
return array(
'success' => true,
'message' => sprintf(
/* translators: %s is the plugin name.*/
esc_html__( 'Plugin "%s" installed and activated successfully.', 'text-domain' ),
$name,
),
);
}
}
// Example usage of prefix_install_plugin function
function test_install_plugin() {
$plugins = array(
array(
'name' => esc_html__( 'Gutentor', 'text-domain' ),
'slug' => 'gutentor',
'plugin' => 'gutentor/gutentor.php',
),
array(
'name' => esc_html__( 'Templateberg', 'text-domain' ),
'slug' => 'templateberg',
'plugin' => 'templateberg/templateberg.php',
'source' => 'https://downloads.wordpress.org/plugin/templateberg.1.1.6.zip',
),
);
foreach ( $plugins as $plugin_info ) {
$result = prefix_install_plugin( $plugin_info );
if ( $result['success'] ) {
echo 'Success: ' . $result['message'];
} else {
echo 'Error: ' . $result['message'];
}
}
exit;
}
add_action( 'init', 'test_install_plugin' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment