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>';
        } );
    }
}

Published by

tuanitpro

Tôi là Lê Thanh Tuấn, và tôi chia sẻ những điều mình cho rằng nó là thú vị, hay giúp ích cho bạn!

Leave a Reply