A screencast adding a section, a setting, and a control with live preview. WordPress rewards developers who work with its APIs instead of around them. Almost every problem we have debugged in client projects started with a shortcut that bypassed a core function.
Sample footage: a CC0 clip from the MDN Web Docs example media.
The WordPress way
Register things on the right hook, pass complete arguments, and let core handle the rest. It is more typing up front, but the result survives updates and plays nicely with other plugins.
- Check capabilities before doing anything.
- Verify a nonce on every state-changing request.
- Sanitise input and escape output, always.
- Script test sites with WP-CLI.
Every endpoint and form needs three things: a capability check, a nonce or equivalent, and sanitisation on the way in with escaping on the way out. Skipping any one of them is how sites get compromised.
None of this is new, and that is the point. The techniques that last are usually the boring ones that are documented well and supported by the platform itself.
As always, the examples are simplified to fit on a page. The production version has more error handling and logging, but the shape of the code is the same.
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Martin Fowler, Refactoring, 1999
Clear, practical, no fluff. More of this, please.
Counterpoint: for small teams the simpler approach in the first section is usually enough.
The quote in the middle is doing a lot of work here, and it earns it.