Home

Awesome

WP Lib Loader

jtsternberg.github.io/wp-lib-loader

Utility template class for smartly loading WordPress code libraries.

This is a handy template class for generating smart loaders for your WordPress code libraries and eliminates conflicts and the need for function_exists or class_exists checks in your plugins/themes when including the bundled library. This loader was inspired by CMB2, and is a proven system. It allows any plugin or theme to bundle your library, and only one instance (the most up-to-date version) will be loaded by the system.

It accomplishes this through a semi-magical use of the WordPress hooks system combined with unique loader class-names for each version of the library.

See the background post: Don’t Repeat Yourself. Use WP Lib Loader instead!

Use the generator tool to quickly create a loader for your library!

Getting Started

To use this template, you will need to <del>copy the contents of the loader.php file to your library and then complete a five-step find and replace on the file</del> head over to the generator tool!. As an example, if your library is named WP_Magic:

  1. Search for LIBCLASSNAME and replace with your library's class-name: WP_Magic.
  2. Search for LIBNAMEUPPER and replace with your library's class-name, upppercased (for constants): WP_MAGIC.
  3. Search for LIBNAMELOWER and replace with your library's class-name, lowercased (for hook names): wp_magic.
  4. Search for LIBURL and replace with your library's URL: https://wp_magic.io.
  5. Search for AUTHORNAME and replace with your name: John Doe.
  6. Search for AUTHOREMAIL and replace with your email: john@johndoe.com.

The resulting file:

examples/wp-magic.php

How to version your library

Since the real magic of this loader is when you release a new version, let's walk through that process. Let's say you're looking to release a bug-fix for WP_Magic.

  1. Increase your version numbers to 0.1.1.

    • Replace the 0.1.0 for the @version docblocks.

      * @version  0.1.1
      
    • Replace the 0.1.0 for the VERSION constant.

      const VERSION = '0.1.1';
      
  2. Update the loader class-name from WP_Magic_010 to WP_Magic_011.

  3. MOST IMPORTANT: Decrement the PRIORITY constant.

    const PRIORITY = 9998;
    

That's it! That's all there is to releasing a new version! You can now be sure that this will be the version used if multiple copies of the library exist.

The resulting file after bumping the version:

examples/wp-magic-version-011.php

Additional details

Super important caveats

Examples in the wild