0

I'm writing an addon which has a main plugin dependency. I use POO so classes and namespaces to do this.

I would like to load an addon after the main plugin and does some check and init it correctly on the right moment.

I found an awful solution was to check if the main plugin is inactive thanks the function is_plugin_inactive. The problem of this solution was you need a hook wich load the addon later and create others problems - example : when you want to use the wp_ajax_ hook.

 namespace MyPlugin;

 class AddonMainClass{

     public function __construct(){
         $this->init(); //<-- Option 1
         add_action('admin_init',  array( $this , 'init' ) ); //<-- Option 2
         add_action('_admin_menu',  array( $this , 'init' ) ); //<-- Option 3 
     }

     public function init(){
         if( is_admin() ){
            if ( is_plugin_inactive( 'main-plugin/MainPlugin.php' ) ) {
               // admin notices    
            }else{
               // Init the addon 
            }
         }
     }
 }

Problems with each option

  • Option 1 : Impossible to use the function is_plugin_inactive() and I'm not sure the main plugin is loaded before.
  • Option 2 : With this hook, my new menu item is not added when I call add_action( 'admin_menu', array( $this, 'load_view_page')); in a class inside my main Plugin to display all menu items.
  • Option 3 : As I said before, the hook _admin_menu seems to be too late to init my plugin so it disturbs some hook loading like wp_ajax_my_function.

I tried to find a totorial to do this on the Web without success.

What is the best way to do this correctly ?

0

The best solution I found to... :

  1. Check if the main plugin is active just before to initialize the addon
  2. init the addon before to add menu item or some hook adding like wp_ajax_my_function

Use the hook plugins_loaded to init addons

Use a function to check in pure PHP classes, functions and constants from the main plugin like here

 namespace MyPlugin;

 class AddonMainClass{

     public function __construct(){
         add_action('plugins_loaded',  array( $this , 'init' ) ); 
     }

     public function init(){
         if( is_admin() ){
              $is_main_plugin_active = $this->detect_plugin(
                array(
                      'classes' => array(   
                              'MyPlugin\\Common\\ClassA',
                              'MyPlugin\\Common\\ClassB'
                      ),
                      'functions' => array(
                              'MyPlugin\\useful_function'
                      ),
                      'constants' => array(
                              'MyPlugin\\PLUGIN_DOMAIN'
                      )
                )
            );
            if ( ! $is_main_plugin_active ) {
               // admin notices    
            }else{
               // Init the addon 
            }
         }
     }

     private function detect_plugin( $plugins ) {

         /** Check for classes */
         if ( isset( $plugins['classes'] ) ) {
             foreach ( $plugins['classes'] as $name ) {
                if ( class_exists( $name ) )
                    return true;
             }
        }

        /** Check for functions */
        if ( isset( $plugins['functions'] ) ) {
            foreach ( $plugins['functions'] as $name ) {
                if ( function_exists( $name ) )
                    return true;
            }
        }

        /** Check for constants */
        if ( isset( $plugins['constants'] ) ) {
            foreach ( $plugins['constants'] as $name ) {
                if ( defined( $name ) )
                    return true;
            }
        }

        /** No class, function or constant found to exist */
        return false;

    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.