When you’re writing code, there’s a desire to not reinvent the wheel. This is a good desire. If someone else has made the code you need, using it is a better use of your time. But there’s a right way and a wrong way to include those common libraries, especially in WordPress themes and plugins.

PHP Is Dumb

Okay, PHP isn’t dumb, but it’s a very top-down/left-right kind of language. The whole reason we tell people to name functions and classes uniquely in WordPress plugins and themes is that if two people name a function the same thing, PHP gets dumb and errors, telling you it can’t define the same function twice.

This means that commonly used PHP libraries should also be uniquely named. To their credit, most do. But still, what happens if two WordPress plugins or themes include the same library? That same error comes back about not defining the same function twice!

What this means is that when using a common library, it’s important that you enqueue it safely and smartly.

Check If The Library Exists

The magic is that you need to detect if the code is already included and not re-include it. PHP gives you two straightforward ways to do this: class_exists and function_exists.

It’s much easier for me when the code is wrapped nicely in a class. Assuming you have a library file that includes the class but doesn’t call it, you can check like this:

if ( !class_exists( 'MyLibraryClass' ) ) {
    include( 'path/to/library.php' );
    $mylibraryclass = new MyLibraryClass();

If it does include the class (with a new MyLibraryClass() in the library) then you can leave that bit out.

When you have a library that perhaps predates classes, you’ll want to pick the primary function and call it like this:

if ( !function_exists( 'my_library_function' ) ) {
    include( 'path/to/library.php' );

What About The Weird Stuff?

There are two things I left out. What about namespaces and javascript libraries?

First of all, it’s impossible to check for the existence of a namespace. You see, a namespace doesn’t really exist. What does exists are the structures within the namespace. That means if you wanted to check for the class ClassName in the namespace of MyName you have to do this:

class_exists( '\MyName\ClassName') );

Second, if you’re looking for javascript libraries, and you’re using WordPress, you don’t have to. Just enqueue it.

Schnieder from One Day at a Time (2017) going 'Mind blown'

The WordPress function wp_enqueue_script() registers the script but does not overwrite and enqueues it.

Now there is a trick to all this. If you use the enqueue command properly, then this always works. By this I mean if you do it this way, it’ll work:

wp_enqueue_script ( 'chartjs', 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js' );

But… A lot of people do this:

wp_enqueue_script ( 'my-plugin-chartjs', 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js' );

And that means WordPress will load both chartjs and my-plugin-chartjs which sucks.

Also you don’t need to append -js (or -css) to your enqueues, as WordPress will do that for you.

Reader Interactions

%d bloggers like this: