Where to put third party PHP library?

The question:

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?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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.

if( class_exists( 'SomeClass' ) )
    // require/include here


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.

Method 2

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.

Method 3

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.


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

Method 4

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.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment