How to integrate plugin in WordPress theme

I have read that there are two ways for integrating a plugin on a WordPress theme.

I think the first method is the best; but I do not know exactly how to do it…

After copying the plugin to the plugin directory of WordPress… How do I enable it? Do you know another way to do this?

Then I expose what I’ve tried to do, that ye may prove if is right what I think:

First method: copy my plugin to wp-content/plugins.

  • Bundling the plugin as is, and using code to copy it to the plugins folder and call activate_plugin to install it.

All I know is how to copy the plugin directory:

function recurse_copy($src,$dst) { 
    $dir = opendir($src); 
    while(false !== ( $file = readdir($dir)) ) { 
        if (( $file != '.' ) && ( $file != '..' )) { 
            if ( is_dir($src . '/' . $file) ) { 
                recurse_copy($src . '/' . $file,$dst . '/' . $file); 
            } else { 
                copy($src . '/' . $file,$dst . '/' . $file); 

// I copy my plugin to wp-content/plugins
recurse_copy($path."/plugins/myplugin_Directory", WP_PLUGIN_DIR . "/myplugin_Directory");

Second method: using my plugin instead a plugins inside wp-content-plugins.

  • Using a copy of the plugin in the theme folder and change the references (paths) to all dependencies for to be used the resources from the theme directory.

Example for the second method: (put this in functions.php)

add_action('after_setup_theme', 'load_MyPlugin');
function load_MyPlugin() { 
    if (!class_exists('MyPluginClass')) {
        include_once(get_template_directory_uri() . '/plugins/myplugin_Directory/index.php');

Solutions Collecting From Web of "How to integrate plugin in WordPress theme"

That depends on what the plugin does, whether your theme relies on it and whether it is your own plugin or not.

For example, when it’s a plugin that adds a custom post type with special theme pages and stuff (like events or a portfolio), than you should integrate it in your theme using your second method.

On the other hand, when it’s a plugin from an external developer (like a slider or something), then you should let the user install that plugin in the /wp-content/plugins folder (or copy it to the plugins folder). A nice help here is the TGM Plugin Activation, which is used by many themes these days. It recommends plugins your theme depends on and can even install and activate them automaticly. It can install them from the WP repository or from a folder when you include a .zip file in your theme somewhere (like in case of a premium plugin).