Master Linting with PHPCS and ESLint to enforce coding standards. Learn to automate quality checks in your build process for a scalable WordPress plugin.
Previously in this course, we explored Code Splitting and Lazy Loading for WordPress React Plugins to optimize our production bundles. Now that we have a performant architecture, we need to ensure our code remains maintainable as the project grows. Linting is the practice of running static analysis tools on your source code to flag programming errors, bugs, stylistic errors, and suspicious constructs.
In a professional WordPress environment, manual code reviews are insufficient. By integrating automated linting, we enforce the WordPress Coding Standards and prevent "code rot" before it hits your version control system.
PHP_CodeSniffer (PHPCS) is the industry standard for tokenizing PHP files and checking them against a defined set of rules. For WordPress, we rely on the wp-coding-standards/wpcs ruleset.
First, install the necessary dependencies via Composer:
Bashcomposer require --dev squizlabs/php_codesniffer wp-coding-standards/wpcs
Once installed, you need to register the WordPress ruleset with PHPCS so it knows how to validate your code:
Bash./vendor/bin/phpcs --config-set installed_paths ../../wp-coding-standards/wpcs
Create a phpcs.xml.dist file in your plugin root. This configuration file ensures every developer on your team uses the exact same rules:
XML<?xml version="1.0"?> style="color:#808080"><style="color:#4EC9B0">ruleset name="KnowledgeBasePluginRuleset"> style="color:#808080"><style="color:#4EC9B0">description>Custom ruleset for the Knowledge Base plugin.style="color:#808080"></style="color:#4EC9B0">description> <!-- Exclude vendor directory --> style="color:#808080"><style="color:#4EC9B0">exclude-pattern>*/vendor/*style="color:#808080"></style="color:#4EC9B0">exclude-pattern> <!-- Use WordPress rules --> style="color:#808080"><style="color:#4EC9B0">rule ref="WordPress-Core" /> style="color:#808080"><style="color:#4EC9B0">rule ref="WordPress-Docs" /> style="color:#808080"><style="color:#4EC9B0">rule ref="WordPress-Extra" /> <!-- Set target PHP version --> style="color:#808080"><style="color:#4EC9B0">config name="testVersion" value="7.4-"/> style="color:#808080"></style="color:#4EC9B0">ruleset>
Since we are building advanced admin interfaces with React, we need to enforce clean JavaScript/TypeScript. We use eslint-config-wordpress, which mirrors the logic found in Configuring ESLint and Prettier for WordPress React Plugins.
Install the required packages:
Bashnpm install --save-dev eslint @wordpress/eslint-plugin
Create an .eslintrc.json file in your plugin root:
JSON{ "extends": [ "plugin:@wordpress/eslint-plugin/recommended" ], "env": { "browser": true, "es6": true }, "rules": { "no-console": "warn" } }
This configuration ensures your React components—like the ones we built in React Component Architecture—adhere to the WordPress JavaScript standard, preventing common pitfalls like missing dependencies in useEffect hooks.
Linting is only effective if it's impossible to ignore. We integrate these commands into our package.json to ensure they run during development and before deployment.
Update your scripts section:
JSON"scripts": { "lint:js": "eslint 'src/**/*.js'", "lint:php": "phpcs src/", "fix:js": "eslint 'src/**/*.js' --fix" }
To take this a step further, integrate these checks into your Git workflow using husky or a simple pre-commit hook. This ensures that no code with syntax errors or style violations can be committed to your repository, maintaining the integrity of our Refactoring for Scalability efforts.
.eslintignore or a // phpcs:ignore comment.phpcs.xml target version matches your production server's PHP version. Running code meant for PHP 8.1 on a server running 7.4 will lead to runtime errors even if the linter passes.WordPress-Core and WordPress-Extra. Custom rulesets should only cover project-specific architectural constraints, not stylistic preferences.phpcs.xml.dist in your Knowledge Base plugin directory../vendor/bin/phpcs src/ and identify at least three violations in your existing code.composer.json to include a lint script that runs both PHPCS and ESLint in a single command.Automated linting is the foundation of a professional plugin. By configuring PHPCS and ESLint, we enforce consistency and catch bugs early. This setup ensures that as we scale our Knowledge Base plugin, the code remains clean, readable, and compliant with WordPress standards.
Up next: We will dive into Unit Testing with PHPUnit to verify our business logic programmatically.
Master Code Quality in your WordPress plugins by configuring ESLint and Prettier. Learn to automate linting and formatting to keep your React code professional.
Read moreMaster the Test-Driven Development workflow in WordPress. Learn to write failing tests, implement clean code, and refactor with confidence for your plugin.
Linting and Code Quality
Custom Hooks for React