Plugin Architecture Patterns

Turn a 2,000-line functions.php into a structure you can test: MVC, layers, a container, repositories and DTOs.

One file, 2,000 lines, and every change breaks something else. A REST route with $wpdb inside, HTML built in the same function, settings read in ten places. This module splits that code into layers you can test, change and explain in a code review.

CoursePHP for WordPress Developers · Module 8 of 9 · Stage 3 "Production code"
LearnMVC and layers in a plugin, bootstrap and container, repository and service, DTOs
HowShort lessons with code from real plugins, IDE practice in the browser, interview questions after every lesson

The idea in one picture

flowchart TD
  H["HTTP layer<br/>REST · admin · hooks"] --> S["Service<br/>business logic"]
  S --> RP["Repository<br/>data access"]
  RP --> DB[("Database")]
  C["Container<br/>bootstrap"] -. builds .-> H
  C -. builds .-> S
  C -. builds .-> RP
  classDef step fill:#f5f3ff,stroke:#777BB4,color:#1e1b4b
  classDef key fill:#777BB4,stroke:#4f5b93,color:#ffffff

Each layer talks only to the one below. A second entry point, like WP-CLI, reuses the service without copying code.

Before → after

BeforeAfter Module 8
$wpdb inside a REST callbackA repository behind a service
HTML built inside business logicA view layer
Arrays with unknown keys passed aroundDTOs and value objects
Every class creates its own dependenciesOne bootstrap with a container

You'll be able to answer

  • How would you split a 2,000-line plugin file?
  • What is the difference between a repository and a service?
  • When is a DI container overkill?