Cover of Refactoring WordPress Plugins part 1

eBook: Refactoring WordPress Plugins: The PHP Parts

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.

Using dependency injection in PHP.

Cover of Refactoring WordPress Plugins part 1

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 Book will take about an hour to read. It’s all based on real code I’ve written and includes Github links whenever possible. This isn’t a book that teaches you how to make a plugin from scratch — not something I get paid do do often. It’s about improving or fixing existing plugins, which are the most common tasks for WordPress plugin developers.

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 plugins easier to extend by third-party developers
  • Pictures of my dog, also flowers.

Also, it’s not very long. You can probably read this in an hour or so.

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 — but 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

Separating Concerns And Decoupling

In this final chapter of part 1, I will walk through a refactor in order to review all of the concepts covered in earlier chapters. I will discuss how I decided where to draw the lines between concerns and show how that guided my decisions around refactoring. In addition, I show how to use integration […]
Read (Members Only): Separating Concerns And Decoupling

Frequently Asked Questions

How Can I Download The Latest Version?

If you have already published the book, go to and log in with the same email address you can download the latest version.

What Formats Is The Book Available In?

Currently, it is a PDF. Members can also read the book online, and leave comments. ePub format is planned, but I can’t get it right yet.

Can I Read This On A Kindle?

Yes, you can send the PDF to your Kindle. This page describes how to send a PDF or ePub to your Kindle