A pond behind some leaves that are on the ground and growing on bushes.

eBook: Refactoring WordPress Plugins Part 1

Using dependency injection in PHP.

The WordPress open-source project is now 20 years old. In that time, PHP and JavaScript have changed a lot. There is still some old code from back then running and it works. That’s great, but if you’re writing PHP and JavaScript for WordPress now, we can do better.

In this 5 part ebook, I walk through different ways you can structure, refactor and test the PHP code you write for WordPress plugins using dependency injection.

This course is an eBook that will take about an hour to read. You can download the book in PDF and ePub format.

Less Foo, No Bar

I am not covering how to build a todo app as a WordPress plugin from scratch. This is about how to improve plugins that already exist.

This book is based on stuff I’m working on, all the code examples are copied from actual work I did. As much as possible, I linked to pull requests where you can see where I implemented these patterns.

Head shot of Jon Bossenger

Josh has taken a complex topic and broken it down in such an accessible way, that I doubt any developer of any level would get to the end of the book and not understand what’s going on. His use of real-world examples of code he’s written for production helps the reader follow the process: from quickly hacking together some code to solve a problem, to refactoring it for prime time.

What makes Josh’s writing stand out from most other technical books is his friendly style of writing, how effortlessly he makes complex topics easy to understand, and the little bits of humor and photography from his personal life sprinkled throughout the book. 

Jonathan Bossenger

WordPress Developer Educator at Automattic



One copy of eBook

Site Membership



Request code samples

Early Access

Josh is the author of about a bajillion WordPress development tutorials, two now three books about WordPress development, and is a contributor to WordPress core and many popular plugins.

What Is In The Book

  • How to remove singletons
  • Code examples based on plugins I’m working on now. Often with links to pull requests.
  • How to write tests with phpunit for WordPress plugins
  • How to make your plugin easier to extend by third-party developers
  • Pictures of my dog, also flowers.

Also, it’s not very long.

By A Guy With ADD For Folks With A Lot Going On

Who has time for a 4-inch thick book or a 25-hour course? I don’t. Maybe beginners do, this isn’t an intro, it assumes you know object-oriented PHP. This should take an hour or two to read.


Singletons & Dependency Injection In WordPress

Singletons used to be very common in WordPress plugins. It used to be that singletons were the only way, besides global variables to have a globally available instance of a class in a WordPress plugin. This section covers why Singletons are not great and when it’s not a big deal. It will cover some ways to refactor plugins so they don’t have singletons.
Read (Members Only): Singletons & Dependency Injection In WordPress

Refactoring A WordPress Plugin To Use A Container

WordPress plugins used to use one big class, which was a singleton, and exposed instances of all of the plugin’s classes. This worked, but singletons have a lot of downsides. It was the best solution in PHP 5.2, which we used to have to support. In the previous section, I wrote about singletons and their […]
Read (Members Only): Refactoring A WordPress Plugin To Use A Container

Refactoring A Class To Make It Reusable Using Dependency Injection

A walk through of how to refactor a PHP class that works and does one thing in order to make it reusable. This is achived with dependency injection and hooks.
Read (Members Only): Refactoring A Class To Make It Reusable Using Dependency Injection

Testing WordPress Hooks

Testing WordPress hooks is always a challenge. Using depenency injection helps. This section covers how to structure hooks and callback functions and how to write unit and integration tests for them.
Read (Members Only): Testing WordPress Hooks

Using “Action” Classes In WordPress Plugins To Encapsulate Logic

How to refactor WordPress plugins to make the code more reusable and easier to test using action classes. Also, how to test WordPress plugins that make HTTP requests to third-party APIs using phpunit.
Read (Members Only): Using “Action” Classes In WordPress Plugins To Encapsulate Logic