// language · backend

PHP for WordPress

WordPress runs on PHP, and so does every plugin. The question is not whether to know PHP but which PHP: the version you write for decides which language features you can use.

  • PHP 8.3+
  • Requires PHP
  • PSR-4
  • WPCS
The PHP elephant 8.3

// 01 — Two PHP worlds

Which PHP do you write for?

One project can hold two PHP worlds. A plugin written for the widest audience cannot use newer language features, even if your own server runs the latest PHP. Pick a target and see what you get.

  1. 7.4typed properties · arrow functions fn() =>
  2. 8.0match · named arguments · union types · nullsafe ?->
  3. 8.1enum · readonly properties · first-class callables
  4. 8.2readonly classes
  5. 8.3typed class constants · #[\Override]
  6. 8.6partial application ? · clamp() · readonly with defaults · what is useful for WordPress →

Decide the minimum version first, and declare it in both the plugin header (Requires PHP) and composer.json.

// 02 — How we organize PHP code

Three habits in our plugins

Namespaces

ForWP\…

Our plugins use ForWP\…, so class names never collide with other plugins.

Autoloading

PSR-4

Some plugins, such as Drive and LMS4WP, load classes through Composer PSR-4 autoload instead of manual require calls.

Strict types

declare(strict_types=1)

Used in MCP Connector, where data passes between agents and the site.

// before: every file by hand
require_once __DIR__ . '/inc/class-api.php';
require_once __DIR__ . '/inc/class-cart.php';
require_once __DIR__ . '/inc/class-log.php';
// after: composer.json
"autoload": {
  "psr-4": { "ForWP\\Shop\\": "src/" }
}

// 03 — Tooling

What checks our PHP

PHPCS + WPCS

WordPress Coding Standards

Plugins
PHPCompatibility

Checks code against the declared PHP versions

Plugins
PHPUnit

Unit tests

Plugins
Pint

Code style

Site
php -l

Syntax check before every commit and deploy

Site
$ php -l inc/class-assets.php
No syntax errors detected in inc/class-assets.php

// 04 — LMS4WP course · 9 modules

PHP for WordPress Developers

WordPress runs on PHP 8. Most WordPress code is still written for PHP 5.6. Modern PHP in the WordPress context, from the runtime to plugin architecture and interview prep.

Open the course

Stage 1 · Language

  1. PHP Runtime in WordPress
  2. Modern PHP Syntax
  3. Callables, Closures and Hooks

Stage 2 · Architecture

  1. OOP for WordPress
  2. Namespaces, Autoloading and Composer
  3. Errors, Exceptions and WP_Error

Stage 3 · Production code

  1. Working with Data
  2. Plugin Architecture Patterns
  3. Interview Prep

From functions.php habits to modern, typed, testable PHP in WordPress.

// 05 — Videos

PHP in short videos

// 06 — FAQ

Questions

Which PHP version should my plugin require?

Decide the minimum version first. For WordPress.org users on older hosting our public plugins mostly require 7.4 and up; newer plugins such as SEO Helper and LMS4WP require 8.0 and up.

Where do I declare the minimum version?

In both the plugin header (Requires PHP) and composer.json.

Why namespaces in a WordPress plugin?

So class names never collide with other plugins. Ours use ForWP\….

How do I know my code still runs on the declared version?

PHPCompatibility checks code against the declared PHP versions; php -l catches syntax errors before every commit and deploy.