Skip to content

Instantly share code, notes, and snippets.

@ericclemmons
Last active June 23, 2023 01:47
Show Gist options
  • Save ericclemmons/6275346 to your computer and use it in GitHub Desktop.
Save ericclemmons/6275346 to your computer and use it in GitHub Desktop.
Auto-activate WordPress Plugins
<?php
// Add to: path/to/wp-content/wp-themes/your-theme/functions.php
/**
* Activate required plugins
*/
include_once ( ABSPATH . 'wp-admin/includes/plugin.php' );
foreach (array(
'plugin-name',
) as $plugin) {
$path = sprintf('%s/%s.php', $plugin, $plugin);
if (!is_plugin_active( $path )) {
activate_plugin( $path );
add_action( 'admin_notices', function() use ($plugin) {
echo '<div class="updated"><p>' . sprintf('<strong>%s</strong> plugin is required & auto-enabled by the current theme.', $plugin) . '</p></div>';
} );
}
}
@knutole
Copy link

knutole commented May 2, 2017

Great, thanks!

@maxyudin
Copy link

maxyudin commented Nov 5, 2017

You don't need to check for is_plugin_active() because activate_plugin() does it itself.

A plugin that is already activated will not attempt to be activated again.

And here is my other suggestion

$result = activate_plugin( $path );

if ( !is_wp_error( $result ) ) {
    add_action( 'admin_notices', function() use ($plugin) {
        echo '<div class="notice notice-success"><p>' . sprintf('<strong>%s</strong> plugin is required & auto-enabled by the current theme.', $plugin) . '</p></div>';
    } );
} else {
    add_action( 'admin_notices', function() use ($plugin) {
        echo '<div class="notice notice-error"><p>' . sprintf('<strong>%s</strong> plugin can\'t be auto-enabled by the current theme.', $plugin) . '</p></div>';
    } );
}

@shiv-dotknow
Copy link

thanks for the feature.

Can we auto activate WP Clone by WP academy plugin. I want this while installing the WP.

Thanks
Shiva

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment