Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kudratullah/2c6dca52b25e39417e23 to your computer and use it in GitHub Desktop.
Save Kudratullah/2c6dca52b25e39417e23 to your computer and use it in GitHub Desktop.
Integrating / Embedding WordPress Plugins Directly Into WordPress Theme.
<?php
/**
* Integratin WordPress Plugins into WordPress Theme for automatic download/instgall and activation.
* Required TGM Plugin Activation library [http://tgmpluginactivation.com/]
* Help Urls--
* http://code.tutsplus.com/tutorials/integrating-the-envato-wordpress-toolkit-to-your-theme-the-plugin--wp-33264
* http://tgmpluginactivation.com/
* https://wordpress.org/support/topic/embed-a-plugin-in-template
* http://thomasgriffinmedia.com/blog/2011/09/automatically-install-plugins-with-themes-for-wordpress/
* http://codex.wordpress.org/Function_Reference/is_plugin_active
* https://pippinsplugins.com/checking-dependent-plugin-active/
*
*/
/**
* Load the TGM Plugin Activator class to notify the user
* to install the Envato WordPress Toolkit Plugin
*/
require_once( get_template_directory() . '/inc/class-tgm-plugin-activation.php' );
function tgmpa_register_toolkit() {
// Specify plugins
$plugins = array(
array(
'name' => 'Envato WordPress Toolkit', // The plugin name.
'slug' => 'envato-wordpress-toolkit-master', // The plugin slug (typically the folder name).
'source' => get_template_directory() . '/plugins/envato-wordpress-toolkit-master.zip', // The plugin source.
'required' => true, // If false, the plugin is only 'recommended' instead of required.
'version' => '1.5', // E.g. 1.0.0. If set, the active plugin must be this version or higher.
'force_activation' => true, // If true, plugin is activated upon theme activation and cannot be deactivated until theme switch.
'force_deactivation' => false, // If true, plugin is deactivated upon theme switch, useful for theme-specific plugins.
'external_url' => '', // If set, overrides default API URL and points to an external URL.
),
// This is an example of how to include a plugin from a private repo in your theme.
array(
'name' => 'TGM New Media Plugin', // The plugin name.
'slug' => 'tgm-new-media-plugin', // The plugin slug (typically the folder name).
'source' => 'https://s3.amazonaws.com/tgm/tgm-new-media-plugin.zip', // The plugin source.
'required' => true, // If false, the plugin is only 'recommended' instead of required.
'external_url' => 'https://github.com/thomasgriffin/New-Media-Image-Uploader', // If set, overrides default API URL and points to an external URL.
),
// This is an example of how to include a plugin from the WordPress Plugin Repository.
array(
'name' => 'BuddyPress',
'slug' => 'buddypress',
'required' => false,
),
);
//set up the options for TGM {http://tgmpluginactivation.com/}
// i18n text domain used for translation purposes
//Change the $theme_text_domain variable to the text domain you are using, or leave it as default.
$theme_text_domain = 'default';
// Configuration of TGM
/**
* Array of configuration settings. Amend each line as needed.
* If you want the default strings to be available under your own theme domain,
* leave the strings uncommented.
* Some of the strings are added into a sprintf, so see the comments at the
* end of each line for what each argument will be.
*/
$config = array(
'domain' => $theme_text_domain,
'default_path' => '',
'parent_menu_slug' => 'admin.php',
'parent_url_slug' => 'admin.php',
'menu' => 'install-required-plugins',
'has_notices' => true,
'is_automatic' => true,
'message' => '',
'strings' => array(
'page_title' => __( 'Install Required Plugins', $theme_text_domain ),
'menu_title' => __( 'Install Plugins', $theme_text_domain ),
'installing' => __( 'Installing Plugin: %s', $theme_text_domain ),
'oops' => __( 'Something went wrong with the plugin API.', $theme_text_domain ),
'notice_can_install_required' => _n_noop( 'This theme requires the following plugin: %1$s.', 'This theme requires the following plugins: %1$s.' ),
'notice_can_install_recommended' => _n_noop( 'This theme recommends the following plugin: %1$s.', 'This theme recommends the following plugins: %1$s.' ),
'notice_cannot_install' => _n_noop( 'Sorry, but you do not have the correct permissions to install the %s plugin. Contact the administrator of this site for help on getting the plugin installed.', 'Sorry, but you do not have the correct permissions to install the %s plugins. Contact the administrator of this site for help on getting the plugins installed.' ),
'notice_can_activate_required' => _n_noop( 'The following required plugin is currently inactive: %1$s.', 'The following required plugins are currently inactive: %1$s.' ),
'notice_can_activate_recommended' => _n_noop( 'The following recommended plugin is currently inactive: %1$s.', 'The following recommended plugins are currently inactive: %1$s.' ),
'notice_cannot_activate' => _n_noop( 'Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.', 'Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.' ),
'notice_ask_to_update' => _n_noop( 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.' ),
'notice_cannot_update' => _n_noop( 'Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.', 'Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.' ),
'install_link' => _n_noop( 'Begin installing plugin', 'Begin installing plugins' ),
'activate_link' => _n_noop( 'Activate installed plugin', 'Activate installed plugins' ),
'return' => __( 'Return to Required Plugins Installer', $theme_text_domain ),
'plugin_activated' => __( 'Plugin activated successfully.', $theme_text_domain ),
'complete' => __( 'All plugins installed and activated successfully. %s', $theme_text_domain ),
'nag_type' => 'updated'
)
);
//nitialize TGM right before the end
tgmpa( $plugins, $config );
}
add_action( 'tgmpa_register', 'tgmpa_register_toolkit' );
/**
* Detect plugin. For use on Front End only.
*/
include_once( ABSPATH.'wp-admin/includes/plugin.php' );
// check for plugin using plugin name
if(is_plugin_active( $plugin)){
//plugin is activated
}
/**
* Or use php function_exists to detect plugin installed or not
*/
if(function_exists('MainFunctionOfPlugin') {
// True
// Use the plugin
}else{
// False
// Display error that plugin is not activate or installed.
// Or use Alternative Content
}
/**
* Or use php class_exists to detect plugin installed or not
*/
if(class_exists('ClassNameOfPlugin')){
// True
// Use the plugin
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment