WordPress Plugin Development Standards: Security and Coding Rules
Build WordPress plugins to the WordPress Coding Standards for PHP, HTML, CSS and JavaScript, treat all input as untrusted, validate and sanitize it, escape output as late as possible, use built-in API functions where they exist and keep the code updated.
This article is for developers who write or review WordPress plugins. It summarizes the official coding standards and the security principles in the WordPress handbook, then states how we apply them.
Coding standards
The WordPress Coding Standards cover four languages, each with its own page: PHP, HTML, CSS and JavaScript. They also set an accessibility goal of WCAG level AA for new and updated code.
Security principles from the handbook
- Treat data from users, third-party APIs and the database as untrusted until verified.
- Validate and sanitize input before you use it, and escape output when you render it.
- Escape as late as possible, and escape data from untrusted or third-party sources.
- Never assume anything about incoming data.
- Prefer validating and rejecting bad data over sanitizing it when you can.
- Use the built-in WordPress API functions for validation and sanitization where they exist.
- Keep code updated, because new vulnerabilities appear over time.
How we build plugins
- We follow the WordPress Coding Standards.
- We write object-oriented PHP.
- We escape and prepare database queries properly.
- We never modify WordPress core files.
Review checklist
- Does every piece of external data pass validation before use?
- Is every output escaped at the point where it is printed?
- Does the plugin use core functions instead of custom code for common jobs?
- Does the code follow the coding standards for each language?
- Do new interface elements meet accessibility level AA?
- Is there a plan to update the plugin when dependencies change?
Common mistakes
- Escaping on input instead of output.
- Trusting data that comes from your own database.
- Rewriting what a core function already does.
- Editing core or third-party plugin files instead of extending them.
Frequently asked questions
What are the WordPress Coding Standards?
The official standards for PHP, HTML, CSS and JavaScript in WordPress projects, plus an accessibility goal of WCAG level AA for new and updated code.
When should I escape output?
As late as possible, at the point where data is printed, and always for data from untrusted or third-party sources.
Should I sanitize or reject invalid input?
The handbook prefers validating and rejecting where you can, and sanitizing when you must accept the data.
Related
Sources
Need this built or fixed?
Start a proposal and our EU-sovereign team will scope it with you.
Start a proposal →