PHP Coding Standards
WooCommerce follows the WordPress Coding Standards for PHP code, with additional WooCommerce-specific requirements.PHP Standards Overview
WordPress Standards
Base standards from WordPress core
WooCommerce Core
Additional WooCommerce-specific rules
PSR-4 Autoloading
Modern code in
src/ directoryPHP Compatibility
Support for PHP 7.4+
Modern vs Legacy Code Architecture
WooCommerce has two distinct code styles based on directory structure:PHP Linting Commands
Run these commands from the WooCommerce plugin directory:The pre-commit hook will automatically run linting on staged PHP files before allowing a commit.
PHPStan Static Analysis
Before committing PHP changes, run PHPStan to catch type errors:PHP Configuration (phpcs.xml)
The rootphpcs.xml file defines the PHP_CodeSniffer rules:
phpcs.xml
PHP Code Requirements
1
Namespace Requirements
All modern code must use the
Automattic\WooCommerce namespace:2
Dependency Injection
Classes in
src/ must use dependency injection via init() method:3
Documentation
All classes and methods must have proper DocBlocks:
4
Type Hints
Use PHP type hints for all parameters and return values (PHP 7.4+):
JavaScript Coding Standards
JavaScript code follows the WordPress JavaScript coding standards via ESLint.JavaScript Linting
ESLint Configuration
The root.eslintrc.js extends WooCommerce ESLint plugin:
.eslintrc.js
JavaScript Code Requirements
- React Components
- TypeScript
- Testing
Code Review Requirements
Before submitting a pull request, ensure your code meets these requirements:Pre-Commit Checklist
- All PHP code passes
lint:php:changeswithout errors - All JavaScript code passes
lint:lang:jswithout errors - PHPStan analysis passes on modified PHP files
- Code follows WordPress and WooCommerce coding standards
- All functions and classes have proper documentation
- Type hints are used for all parameters and return values (PHP)
- No hardcoded text strings (use i18n functions)
- Security best practices are followed (nonce verification, sanitization, escaping)
- Code supports minimum PHP (7.4+) and WordPress (5.2+) versions
Pre-Commit Hooks
The repository includes Husky pre-commit hooks that automatically run:package.json (lint-staged)
The hooks will automatically fix many issues and prevent commits that don’t meet standards.
Editor Configuration
EditorConfig
Use EditorConfig to automatically configure your editor:.editorconfig
Common Standards Issues
Text Domain
All translatable strings must use thewoocommerce text domain:
File Naming
- Modern Code (src/)
- Legacy Code (includes/)
Escaping and Sanitization
Next Steps
Testing
Learn about testing requirements and commands
Git Workflow
Follow our Git workflow for contributions