One of my WordPress themes requires a few third party plugins to run correctly.
Most of the times I used to call functions from third party plugins using conditional statements like
if(function_exist('plugin_function')) {
plugin_function() // do something
}
suppose though I need to use extensively one plugin through many files of my theme… I would like to avoid using many IF conditions… is there a proper way to require certain specific plugin to be installed in WP or even better install them if they’re missing before activating the theme?
thanks
is_plugin_active()
is rather fragile: it will break when either the plugin author renames the main file or when the user renames the plugin’s directory or main file. It’s better to check if a certain public function exists.
To avoid having to make that check each time you need some of the plugin’s functionality, you could show a message in the admin area:
add_action( 'admin_notices', 'my_theme_dependencies' );
function my_theme_dependencies() {
if( ! function_exists('plugin_function') )
echo '<div class="error"><p>' . __( 'Warning: The theme needs Plugin X to function', 'my-theme' ) . '</p></div>';
}
Another alternative is to use something like http://tgmpluginactivation.com/