Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.

Any way to check if registered plugin exists

+4 votes
249 views
asked Apr 5, 2013 in Plugins by jatin.soni
Found one issue where plugin layer has custom query for custom column and if column and  plugin doesn't exists definatly it will gives an error. But in some case it mess up the layout too.

So is there any way to check if plugin exists same like function_exists() ? so that code we can wrap around that function and if plugin doesn't exists than just ignore it.
Q2A version: plugin-api, check, conditional
commented Feb 12, 2014 by q2apro
Hi Jatin, could you find another solution? I am still wondering which approach is the best. The function_exists() approach below is not recommended as the function will only exist if the plugin has been called beforehand. Really need a plugin_exists() function!
commented Feb 12, 2014 by jatin.soni
Find my below answer.. may be help you

2 Answers

0 votes
answered Apr 7, 2013 by q2apro

you can use function_exists with one of the functions' names in the plugin.

For checking if the badges plugin is installed, for instance, I am using this in another plugin:

                // output of badges, needs badges plugin
                echo (function_exists('qa_badge_plugin_user_widget') ? '<br/>'.qa_badge_plugin_user_widget($handle) : '');

 

where qa_badge_plugin_user_widget' is the function of badges plugin.

commented Apr 7, 2013 by jatin.soni
I think this can be alternative but no exactly what I am looking.. What I want to specifically check either pluign exist or not. And that will apply to every single piece of code for my all files. Means instead of wrapping individual function to check I want one function same like function_exists where it can check plugin_exists(). This is not only to check for function. But if I have custom query or something more than juts function where function_exists() may not works.
+1 vote
answered Feb 12, 2014 by jatin.soni
edited Feb 12, 2014 by jatin.soni

Since Q2A Plugin is all about the class and any function out of the class, those too have to include within the class. What I believe that if we check if the plugin class exists that may be alternative of the plugin_exists() function.

But here is one more problem. Q2A plugin can contain multiple class for layer, module etc.. However layer will use qa_html_theme_base class and so it is not good idea to check for that class, since it is always exists. But the plugin class.

See below. I haven't practically tried this but logically it should work

<?php
//registering plugin stuffs
qa_register_plugin_layer('q2am-item-output.php', 'Q2AM Item Layer');
qa_register_plugin_module('page', 'q2am-item-page.php', 'q2am_item_page', 'Q2AM Item Page');
qa_register_plugin_module('module', 'q2am-item-admin.php', 'q2am_item_admin', 'Q2AM Item Settings');
qa_register_plugin_phrases('q2am-Item-lang.php', 'q2am_item_lang');

if(class_exists('q2am_item_page'))
{
    //do the stuffs
}

if(class_exists('q2am_item_page') && class_exists('q2am_item_admin') ....)
{
    //do the stuffs
}

//May be we can get list of classes and check in_array

$classes = get_declared_classes();

if(in_array('q2am_item_page', $classes ))
{
    //do the stuffs
}

 

Find on Gist here https://gist.github.com/q2amarket/8951524

 


Edit: Once class confirmed than you can check function_exists() within the class

commented Feb 12, 2014 by q2apro
I guess this is exactly the reason why sama55 created a plugin:

"This plugin(required core hack) changes plugin execution order." → http://www.question2answer.org/qa/23788/new-plugin-plugin-order-required-core-hack?show=23788#q23788
commented Feb 12, 2014 by jatin.soni
I have already requested few important things.. plugin_exists(), functions and layer priority like wordpress add_action($priority); and few other stuffs. Without it definitely going to create lot of issue end of the day.

We are getting now more and more plugins, themes, hook and same method and functions been overridden again and again which creates conflict.

Hope things will be sorted out in next version. Meantime I will try to find some solution.. just need some free time..

BTW can you upload your code to github or may be on bitbucket creating private repo where I can check it?
...