Skip to content
InfinityKnow

InfinityKnow

Infinity Knowledge (IK) : Technology, Articles, Topics, Facts or many More.

  • Home
  • Education
    • yttags
    • Make Money
    • Jobs
    • Programming
      • Technology
      • Web Design
      • WEB HOSTING
      • Interview
  • Entertainment
    • pakainfo
    • Sports
    • Tips and Tricks
      • Law
      • Photography
      • Travel
  • Health
    • Insurance
    • Lifestyle
      • Clothing
      • Fashion
      • Food
  • News
    • Insurance
      • Auto Car Insurance
      • Business Insurance
    • Donate
    • California
  • News
    • Political
  • Home Improvement
  • Trading
    • Marketing
    • Top Tranding
    • Business
    • Real Estate
  • Full Form
  • Contact Us
  • Hire PHP Developers For Web Development In Australia
    Hire PHP Developers For Web Development In Australia PHP
  • OCCASION
    FOR EVERY OCCASION, THERE ARE DIFFERENT KINDS OF BRACELETS Articles
  • Quotes about life Quotes
  • Cleaning
    An Educational Guide to Cleaning: The Best Tips to Keep Your Home Clean Articles
  • Angularjs math expression – number expressions Example
    Angularjs math expression – number expressions Example Technology
  • Vuejs file upload Ajax formdata component Example
    Vuejs file upload Ajax formdata component Example Technology
  • ASP.NET UpdateProgress control Tutorial with Examples Technology
  • Top AngularJS Interview Questions and answers
    Top AngularJS Interview Questions and answers Technology

WordPress plugin dependencies on Activation

Posted on September 4, 2019 By admin No Comments on 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 :  Angular Dynamically Delete Row from Table

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() );
}

/**
* @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:

READ :  angularbind - AngularJS Bind Function - Angular Binding Examples

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;
}

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.

READ :  ASP.NET MVC Advantage Tutorial with Examples

[/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 :

  • Jobs
  • Make Money
  • Programming

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.

Related posts:

  1. Remove Yellow BGBOX WordPress plugin
  2. WordPress Remove Yellow BGBOX – donate
  3. Vuejs Simple Tooltip Plugin v-tooltip Example
  4. how to check a plugin is activated or not in wordpress
PHP, Programming, Technology, wordpress Tags:woocommerce product dependencies, wordpress active plugin, wordpress check if plugin is used, wordpress is plugin, wordpress load plugin after another, WordPress plugin dependencies Require another plugin, wordpress plugin development, wordpress plugin require another plugin, wp plugin activated

Post navigation

Previous Post: Count total number of items in nested array in PHP
Next Post: Redirect to custom page after plugin activation

Related Posts

  • Vue.js Routing With vue-router
    Vue.js Routing With vue-router Technology
  • Angular Call Function on Page Load Example Technology
  • Sorting and Searching using Vuejs – Vuejs table sort pagination
    Sorting and Searching using Vuejs – Vuejs table sort pagination Technology
  • Vuejs Simple Line Chart using JSON - javascript
    Vuejs Simple Line Chart using JSON – javascript Technology
  • Vuejs Http Get Method with parameters – Vue-Resource Get Request
    Vuejs Http Get Method with parameters – Vue-Resource Get Request Technology
  • Angular http POST pass Multiple Parameters PHP MySQLi Technology

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Account web hosting (1)
  • AngularJs (277)
  • Articles (143)
  • Asp.Net (49)
  • Astrology (2)
  • Attorney (7)
  • Auto Car Insurance (4)
  • Biography (2)
  • Business (9)
  • Business Insurance (3)
  • California (4)
  • Choose the web hosting (1)
  • Clothing (6)
  • cloud (8)
  • Cloud data storage (2)
  • Credit (1)
  • Dedicated hosting server web (1)
  • Dedicated server web hosting (1)
  • Dedicated web hosting (1)
  • Degree (11)
  • Design (9)
  • Differences shared hosting (1)
  • Donate (2)
  • Education (37)
  • Energy web hosting (1)
  • Entertainment (6)
  • Facts (12)
  • Fashion (3)
  • Finance (3)
  • Food (5)
  • full form (90)
  • Google Adsense (22)
  • Health (20)
  • Home Improvement (5)
  • Insurance (6)
  • Interview (2)
  • Jobs (6)
  • jquery (2)
  • jQuery (2)
  • Laravel (164)
  • Lawyer (4)
  • Lifestyle (6)
  • Loans (6)
  • Make Money (31)
  • Managed dedicated server (1)
  • Managed hosting solution (1)
  • Managed servers (1)
  • Marketing (8)
  • Mortgage (2)
  • Movies (21)
  • MySQL (180)
  • News (5)
  • Photography (1)
  • PHP (250)
  • Programming (18)
  • Quotes (75)
  • Real Estate (2)
  • SEO (9)
  • Shared web hosting (1)
  • Shayari (67)
  • Sports (5)
  • Status (34)
  • Stories (45)
  • suvichar (8)
  • Tech (3)
  • Technology (675)
  • Tips and Tricks (42)
  • Top Tranding (35)
  • Trading (28)
  • Travel (12)
  • Uncategorized (8)
  • VueJs (179)
  • Web Design (2)
  • WEB HOSTING (1)
  • Web hosting company (1)
  • Web hosting really (1)
  • Web hosting windows (1)
  • Which website hosting (1)
  • Wishes (13)
  • wordpress (15)

Categories

AngularJs (277) Articles (143) Asp.Net (49) Attorney (7) Business (9) Clothing (6) cloud (8) Degree (11) Design (9) Education (37) Entertainment (6) Facts (12) Food (5) full form (90) Google Adsense (22) Health (20) Home Improvement (5) Insurance (6) Jobs (6) Laravel (164) Lifestyle (6) Loans (6) Make Money (31) Marketing (8) Movies (21) MySQL (180) News (5) PHP (250) Programming (18) Quotes (75) SEO (9) Shayari (67) Sports (5) Status (34) Stories (45) suvichar (8) Technology (675) Tips and Tricks (42) Top Tranding (35) Trading (28) Travel (12) Uncategorized (8) VueJs (179) Wishes (13) wordpress (15)
  • Sorts of travel insurance Available Nowadays Articles
  • AngularJs Global Constants Set and Get Variables
    AngularJs Global Constants Set and Get Variables Technology
  • udf full form – udf Kya Hai, Meaning and Abbreviation – What is the full form of udf? full form
  • Set textarea directive value in Angular Technology
  • Angular ng-app directive Example Technology
  • Vuejs Game Programming – fastest click GAME
    Vuejs Game Programming – fastest click GAME Technology
  • Angular Top 10 Example for Beginners Technology
  • scarves
    Sorts of scarves dependent on style  Articles

Copyright © 2022 InfinityKnow.

Powered by PressBook News WordPress theme