WordPress plugin dependencies on Activation

Today, We want to share with you WordPress plugin dependencies on Activation.In this post we will show you wordpress plugin require another plugin, hear for woocommerce product dependencies we will give you demo and example for implement.In this post, we will learn about WordPress plugin dependencies Require another plugin with an example.

WordPress plugin dependencies on Activation

There are the Following The simple About WordPress plugin dependencies on Activation Full Information With Example and source code.

As I will cover this Post with live Working example to develop wordpress load plugin after another, so the wordpress check if plugin is used for this example is following below.

How to check WordPress plugin dependencies

Sometimes a WordPress plugin We are writing extends the functionality of another plugin or simply relies on it to work. In these cases, We need to make sure your plugin’s dependencies are installed and active to be sure We can use their functions.

There are some existing libraries that We can use to make sure that a plugin is running – one such library is TGM Plugin Activation. Checking if another plugin is running, however, is fairly simple and We can implement it yourself if We don’t want to pull an external library for the purpose.WordPress plugins that we make are meant to extend or build on other custom plugins that already exist : .

Ideally, this will take place in a few step process:

  • Check if Plugin dependency is installed and activated.
  • If it is, activate the plugin as usual.
  • If the dependency is not active, do the following:
  • 1)Display the user in an informative way, without throwing a PHP error
  • 2)Deactivate the plugin to prevent any other issues.
READ :  C# Collections Tutorial with Examples

How to check if another plugin is running

WordPress provides a function that allows us to check if a plugin is running. It requires We to load extra PHP source code that is only available in the admin dashboard by default:

[php]
include_once( ABSPATH . ‘wp-admin/includes/plugin.php’ );
if ( is_plugin_active( ‘path to plugin file’ ) ) {
//beta-yoast-seo Plugin is active
}
[/php]

What We can do instead is what this wordpress WooCommerce Plugin guide advises:

[php]
$active_plugins = apply_filters( ‘active_plugins’, get_option( ‘active_plugins’ ) );
if ( in_array( ‘woocommerce/woocommerce.php’, $active_plugins ) ) {
//beta-yoast-seo Plugin is active
}
[/php]

Checking if all required plugins are active

We can now create a class that will allow us to check if all required plugins are running and throw an exception (that we will catch later) if they are not:

beta-yoast-seo/includes/Dependency_Checker.php
[php]
‘Path to main plugin file’
*/
const REQUIRED_PLUGINS = array(
‘Akismet’ => ‘akismet/akismet.php’,
‘WooCommerce’ => ‘woocommerce/woocommerce.php’,
);

/**
* Check if all required plugins are active, otherwise throw an exception.
*
* @throws Beta_Yoast_Seo_Missing_Dependencies_Exception
*/
public function check() {
$missing_plugins = $this->get_missing_plugins_list();
if ( ! empty( $missing_plugins ) ) {
throw new Beta_Yoast_Seo_Missing_Dependencies_Exception( $missing_plugins );
}
}

/**
* @return string[] Names of plugins that we require, but that are inactive.
*/
private function get_missing_plugins_list() {
$missing_plugins = array();
foreach ( self::REQUIRED_PLUGINS as $plugin_name => $main_file_path ) {
if ( ! $this->is_plugin_active( $main_file_path ) ) {
$missing_plugins[] = $plugin_name;
}
}
return $missing_plugins;
}

/**
* @param string $main_file_path Path to main plugin file, as defined in self::REQUIRED_PLUGINS.
*
* @return bool
*/
private function is_plugin_active( $main_file_path ) {
return in_array( $main_file_path, $this->get_active_plugins() );
}

READ :  VueJS Features and Benefits - Vuejs advantages and disadvantages

/**
* @return string[] Returns an array of active plugins’ main files.
*/
private function get_active_plugins() {
return apply_filters( ‘active_plugins’, get_option( ‘active_plugins’ ) );
}

}
[/php]

And here is the exception that our class throws:

beta-yoast-seo/includes/exceptions/Missing_Dependencies_Exception.php
[php]
required_name_of_plugins = $required_name_of_plugins;
}

/**
* @return string[]
*/
public function get_required_name_of_plugins() {
return $this->required_name_of_plugins;
}

}
[/php]

… and the abstract class it extends:

beta-yoast-seo/includes/exceptions/Exception.php
[php]
Suppressing plugin activation if dependencies are not met

If our dependencies are not met, we can prevent our plugin from executing any actual functionality. In a class that sets up our plugin, we can do the following:

beta-yoast-seo/includes/Setup.php
[php]
load_includes();
$this->create_instances();

try {
$this->dependency_checker->check();
} catch ( Beta_Yoast_Seo_Missing_Dependencies_Exception $e ) {
return;
}

// Do actual plugin functionality registration here – add_action(), add_filter() etc.
}

private function load_includes() {
// Exceptions
require_once dirname( __FILE__ ) . ‘/exceptions/Exception.php’;
require_once dirname( __FILE__ ) . ‘/exceptions/Missing_Dependencies_Exception.php’;

// Dependency checker
require_once dirname( __FILE__ ) . ‘/Dependency_Checker.php’;
}

private function create_instances() {
$this->dependency_checker = new Beta_Yoast_Seo_Dependency_Checker();
}

}
[/php]

Reporting missing dependencies to admins

The PHP Source code we wrote therefor far will check if (Akismet, woocommerce) required plugins are running and only bind any actual functionality if they are. It would also be nice if we got a admin notice in the admin dashboard if the required plugins are not running.

To achieve this, we will make a another class that will take care of reporting missing plugins to users with the activate_plugins capability (therefor that only user roles who can actually do something about it – and probably the only ones that should know about it – know about the issue). Only Main Admin have the activate_plugins capability by default.

beta-yoast-seo/includes/Missing_Dependency_Reporter.php
[php]
required_name_of_plugins = $required_name_of_plugins;
}

READ :  where in best laravel - use where Query in Laravel?

public function bind_to_admin_hooks() {
add_action( ‘admin_notices’, array( $this, ‘display_admin_notice’ ) );
}

public function display_admin_notice() {
if ( ! current_user_can( self::REQUIRED_CAPABILITY ) ) {
// If the user does not have the wordpress “activate_plugins” capability, do nothing.
return;
}

$required_name_of_plugins = $this->required_name_of_plugins;
include dirname( __FILE__ ) . ‘/../views/missing-dependencies-admin-notice.php’;
}

}
[/php]

Now, the view to display the actual relavents message:

beta-yoast-seo/views/missing-dependencies-admin-notice.php
[php]

Error:
The Beta Yoast Seo plugin won’t execute
because the following required plugins are not active:
.
Please activate these plugins.

[/php]

And then we have to make our plugin setup class run the Missing_Dependency_Reporter class whenever the dependencies are missing. Let’s rewrite our simple calling the Setup class:

beta-yoast-seo/includes/Setup.php (updated)
[php]
load_includes();
$this->create_instances();

try {
$this->dependency_checker->check();
} catch ( Beta_Yoast_Seo_Missing_Dependencies_Exception $e ) {
// The exception contains the names of missing plugins.
$this->report_missing_dependencies( $e->get_required_name_of_plugins() );
return;
}

// Add here code Like as : – add_action(), add_filter() etc.
// Do actual plugin functionality registration here – add_action(), add_filter() etc.
}

private function load_includes() {
//wordpress Exceptions
require_once dirname( __FILE__ ) . ‘/exceptions/Exception.php’;
require_once dirname( __FILE__ ) . ‘/exceptions/Missing_Dependencies_Exception.php’;

//wordpress Dependency checker
require_once dirname( __FILE__ ) . ‘/Dependency_Checker.php’;
require_once dirname( __FILE__ ) . ‘/Missing_Dependency_Reporter.php’;
}

private function create_instances() {
$this->dependency_checker = new Beta_Yoast_Seo_Dependency_Checker();
}

/**
* @param string[] $required_name_of_plugins
*/
private function report_missing_dependencies( $required_name_of_plugins ) {
$missing_dependency_reporter = new Beta_Yoast_Seo_Missing_Dependency_Reporter( $required_name_of_plugins );
$missing_dependency_reporter->bind_to_admin_hooks();
}

}
[/php]

After that finally, to run our “Checking for a Plugin Dependency on Activation” plugin’s Setup class:

beta-yoast-seo/beta-yoast-seo.php
[php]
init();
}
[/php]

And that’s it! We have a fully-functional wordpress custom plugin dependency checker.

Please let me know if you liked this Article and if it was useful for you. Maybe you know of a better approach? I would love to hear about it!

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about WordPress plugin dependencies on Activation.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment