9

I'm developing a couple of open-source plugins and a theme (all part of a "suite") that all use the same third party PHP library. I'm wondering what is the best way to include it in Wordpress. Here are some thoughts:

  • put it in one of the plugins and require that plugin to be installed and activated
  • create a special "core" plugin that does nothing but including it
  • put it directly in wp-content

Any thoughts on that matter?

2
  • Is this development for private use on single site or multiple sites / pubic release?
    – Rarst
    Oct 21, 2011 at 13:21
  • It's for a suite of open source plugins. I'm updating the question to point that out.
    – gou1
    Oct 21, 2011 at 13:31

4 Answers 4

5

If each plugin/theme functions on its own, then you should probably drop the the library in every theme/plugin.

Then just check to see if it a class or function from the third-party library exists before requiring it.

<?php
if( class_exists( 'SomeClass' ) )
{
    // require/include here
}

or

<?php
if( function_exists( 'some_function' ) )
{
   // Require/include stuff here
}

Alternatively, you could wrap every function/class/variable/constant from the third party library in a check to see if it exists, like pluggable functions.

If all of the plugins and the theme are dependent on one another, then it doesn't really make much sense to divide them up and you should probably rethink that.

4
  • Plugins have different features, hence the separation: you only activate what you want. But it all relies on a framework, that why I need to include that library. It has an autoloader, and quite a few classes so checking/requiring for every class would really be a pain. And I can't really drop it in every class because it would register the autoloader several times. Right now the better solution seems to be the "core" plugin. You activate it first so it requires all the 3rd party stuff, and then you select the plugins you want.
    – gou1
    Oct 21, 2011 at 14:13
  • You're thinking about this wrong: just make a bootstrap file, including something you can check, that includes/requires all the classes. Then require that file. One check. Don't count on your users to figure out they need to install second plugin. Oct 21, 2011 at 14:24
  • That's precisely my question: whatever the method (plugin, bootstrapper, etc.) where to put the files?
    – gou1
    Oct 21, 2011 at 14:38
  • Oh, I would put them in a library folder in each plugin/theme. Then in your main plugin file, run the check and include them if necessary. Oct 21, 2011 at 14:56
3

Bind all depended code to an action in the library plugin.

Sample code for the library plugin:

add_action( 'plugins_loaded', 'load_library', 0 );

function load_library()
{
    # load all the classes and files you need
    # Set up $plugin_url and $plugin_directory

    do_action( 'library_loaded', $plugin_url, $plugin_directory );
}

In your depended code do nothing before the action was called:

add_action( 'library_loaded', 'start_my_code', 10, 2 );

function start_my_code( $lib_url, $lib_directory )
{
    # do stuff
}

The library handles all the basic parts: check for proper PHP version, WordPress constants, multi-site setups etc.

The other code will do nothing if the action 'library_loaded' is not called.

1

Adding to answer by chrisguitarguy, if your libraries are in the form of PHP classes, then you can utilise spl_autoload_register() to load those classes if they haven't already been loaded by another plugin. You can then bundle the libraries into your plugin and just use them, relying on the class loader to include them when appropriate. You can also use the class loader to load your own plugin's classes.

e.g.

define('WPSE_31726_PLUGIN_ROOT', dirname(__FILE__) . '/');

/**
* autoload classes as/when needed
* @param string $class_name name of class to attempt to load
*/
function wpse_31726_autoload($class_name) {
    static $classMap = array (
        'Wpse31726_Admin'       => 'class.Wpse31726_Admin.php',
        'Wpse31726_CsvLoader'   => 'class.Wpse31726_CsvLoader.php',
        'Wpse31726_Plugin'      => 'class.Wpse31726_Plugin.php',
        'parseCSV'              => 'lib/parsecsv/parsecsv.lib.php',
    );

    if (isset($classMap[$class_name])) {
        require WPSE_31726_PLUGIN_ROOT . $classMap[$class_name];
    }
}

// register function for autoloading required classes
spl_autoload_register('wpse_31726_autoload');
2
  • The problem here is not autoloading but "all use the same third party PHP library."
    – gou1
    Feb 15, 2013 at 15:57
  • 1
    Per chrisguitarguy, each could have its own copy of the library, and thus operate essentially independently. As each is called upon to operate, and thus require the library, they load it if it hasn't already been loaded. Autoloading classes merely provides a convenient way to address the "load it if it hasn't already been loaded" bit.
    – webaware
    Feb 15, 2013 at 22:51
0

Since no official vendor directory exists, I would go for the "core" plugin that does nothing but include the library. You then make your plugins require that core plugin.

Putting the library in one of your real plugins would require the user to have that plugin enabled even though they may never want to use its functionality. A separate core plugin seems cleaner.

Putting it directly in wp-content looks like the worst solution.

1
  • That's what I was thinking at first too, but I found out it's not sufficient to just put the including of your libraries in one activated plugin: sometimes (eg. when activating it) the file of the "core" plugin won't be required.
    – gou1
    Oct 21, 2011 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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