0

I have a PHP class that I want to use in several plugins, rather than add the class to each plugin I want to add the class as a plugin and access it that way. This is more OOP and reduces duplication.

What is the best way to go about this?

Safety issues can be ignored, I can write checks to make sure a plugin is available before trying to use it.

The class I am trying to add is an Excel document creator in XML format. It would normally be used as follows.

include("excelwriter.inc.php");

$excel=new ExcelWriter("myXls.xls");
0

You can check if the class is already include / exists, and if it not exists include it.

In your plugin, depending on how you can be sure that the install has plugin activated, you'll maybe need to include your class inw every plugin.

 if(!class_exists('Excel_Writer'){
    include_once('excelwriter.inc.php');
    $excel = new Excel_Writer('myxls.xls');
 }

Be carefull with files paths.

If you really want to include your file only in one plugin, the main plugin (with the required file) must be load first.

Then the other plugin will be able to find and work with the class.

You can add some code to desactivate "sub" plugins if the main is not available with is_plugin_active

Hope it gives you some hints to help you !

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.