WordPress Coding Standards

repository·develop·Indexed 25 days ago

https://github.com/wordpress/wordpress-coding-standards

A collection of PHP_CodeSniffer rules (sniffs) used to validate code against official WordPress coding conventions for core, plugins, and themes. It includes standard subsets such as WordPress-Core, WordPress-Docs, and WordPress-Extra, and supports integration via Composer, custom ruleset files, and CI pipelines.

Tokens
1.1K
Snippets
4
Records
11
Agent score
34%

What's inside WordPress Coding Standards

  1. Use a custom ruleset file

    develop

    You can customize your coding standard selection by creating a configuration file in your project root. If you name the file one of the following, PHP_CodeSniffer will automatically detect it without needing the --standard CLI argument:

    • .phpcs.xml
    • phpcs.xml
    • .phpcs.xml.dist
    • phpcs.xml.dist
  2. Install WordPressCS globally via Composer

    develop

    To install WordPress Coding Standards globally on your system, use the following commands. This allows you to use the standards across multiple projects.

    composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
    composer global require --dev wp-coding-standards/wpcs:"^3.0"
  3. Install WordPressCS locally in a project via Composer

    develop

    To install WordPress Coding Standards as a development dependency within a specific project, run the following commands from your project root. This requires the dealerdirect/phpcodesniffer-composer-installer plugin to be enabled via Composer configuration.

    composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
    composer require --dev wp-coding-standards/wpcs:"^3.0"
  4. Integrate PHPCompatibilityWP into a custom ruleset

    develop

    To check for cross-version PHP compatibility within a WordPress context, add the PHPCompatibilityWP rule to your custom [.]phpcs.xml[.dist] file. It is highly recommended to set the testVersion property to 7.2- to support the last three WordPress releases.

    <config name="testVersion" value="7.2-"/>
    <rule ref="PHPCompatibilityWP">
        <include-pattern>*.php</include-pattern>
    </rule>
  5. Use the WordPress.Utils.I18nTextDomainFixer sniff

    develop

    The WordPress.Utils.I18nTextDomainFixer is a tool within the Utils sniff category designed to replace text domains across a codebase, including I18n function calls and plugin/theme headers.

    Note: Sniffs in the Utils category are disabled by default and are considered risky. They must be activated via a custom ruleset by providing specific properties.

    To activate this sniff, include the following properties in your custom ruleset:

    • old_text_domain: An array containing one or more old text domain names to be replaced.
    • new_text_domain: A string representing the new, correct text domain.
  6. Select WordPress Coding Standard subsets

    develop

    WordPressCS provides several standard names to select specific levels of scrutiny when invoking phpcs:

    • WordPress: The complete set containing all sniffs.
    • WordPress-Core: The main ruleset for WordPress core coding standards.
    • WordPress-Docs: Additional ruleset for WordPress inline documentation standards.
    • WordPress-Extra: An extended ruleset including WordPress-Core plus recommended best practices not covered by core standards.