WordPress: activate_plugin function doesn’t find the file

I’m trying to activate a plugin from the theme’s functions.php, however I get the error ‘Plugin file does not exist’ even though the path is set correctly.
Here’s my code:

function activate_plugins( $plugin_path ) {

    // Get already-active plugins   
    $active_plugins = get_option('active_plugins');
    // Make sure your plugin isn't active
    if (isset($active_plugins[$plugin_path]))
        return;

    // Include the plugin.php file so you have access to the activate_plugin() function
require_once(ABSPATH .'/wp-admin/includes/plugin.php');
    // Activate your plugin
    $res = activate_plugin($plugin_path);
    if ( is_wp_error( $res ) ) {
        echo $res->get_error_message();
    }

    return null;
}
activate_plugins( get_bloginfo('template_directory') . '/plugins/multiple-post-thumbnails/multi-post-thumbnails.php' );

I am trying to load the plugin from the theme folder because I’m currently developing a theme which I want to sell in the future, and don’t want the customer to have to install the plugin manually after installing the theme. What can I do?

Related posts

Leave a Reply

1 comment

  1. Plugins must be contained within the WP plugins directory ./wp-content/plugins/. Looking at your code you are trying to load your plugin from a “plugins” folder located inside the template directory.

    Instead move your plugin to the correct directory as specified above and use ABSPATH . PLUGINDIR to get the correct path.

    activate_plugins( ABSPATH . PLUGINDIR . '/multiple-post-thumbnails/multi-post-thumbnails.php' );