Daryll Doyle WordPress Development and Consultancy in Cornwall

PSR-4 Autoloading in WordPress

P

Recently I’ve been tasked with integrating a few API’s into WordPress sites. Whilst this doesn’t seem too hard, I found it hard to find solid documentation on the best practices in this scenario.

In this post I will explain how I decided to go about these integrations, focussing mainly on my class loading inside WordPress.

Why Autoload?

Themes in WordPress all seem to be very procedural. This is fine for a little theme, but as soon as you start making a few changes, that functions.php file becomes unwieldy pretty quick. I’ve seen people split up their functions files into separate files, each holding functions for a certain part of the site. That’s not bad, but then you’ve got to make sure you include them all into your functions.php file which looks horrendous!

By structuring your code properly, you can move your function calls into classes, these classes can then be autoloaded using PHP’s spl_autoload_register function. Autoloading means no more including files, as long as your register function knows where to look for the code, it will include is for you. Doesn’t that sound good?

How it’s done

Rather than write my own autoloader, I opted to use the Class Example autoloader, defined by the php-fig, which is available here. I add this file, as autoloader.php to my theme root, this is then included into the top of the functions.php file.

In the demo below, I have a folder called Services that holds all of my API classes, these classes are then in turn contained within the services namespace. On line 16 I am passing this namespace into the autoloader and then telling it the location of these classes. Remember, as they are in the theme directory, we need to prefix the folder with the theme directory location, hence the use of the get_template_directory() function.

You can call the addNamespace method as many times as you like with different namespaces and directories, if your classes are split up a little more.

Now what?

Now we’ve registered our autoloader we can call classes as and when we need them without having to worry about including the file each time, whether that be from the functions.php file or from inside a template, it doesn’t really matter as they’re always available. You will however have to call the class with it’s full namespace or make use of the use statement in PHP to import that namespace.

Further Reading

If you’d like to know more about namespacing and autoloading classes, there are some great resources below:

About the author

Daryll Doyle

Daryll is a Staff Engineer at 10up.

With a deep passion for web development and open-source technology, Daryll is the original author of the SVG sanitization plugin Safe SVG, which boasts over 900,000 installs from the WordPress.org plugin directory and is now proudly maintained by 10up.

Throughout his career, Daryll has demonstrated expertise in SVGs and WordPress, sharing his knowledge through public speaking engagements, including a notable talk at WordCamp London 2018.

Beyond his professional life, Daryll enjoys spending quality time with his family and diving into research on Developer Experience, constantly seeking ways to improve the workflow and productivity of fellow developers.

7 comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Daryll Doyle WordPress Development and Consultancy in Cornwall