### Install PEAR Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to install PEAR locally in XAMPP's PHP directory. ```bash C:\xampp\php>php go-pear.phar ``` -------------------------------- ### Command Line Usage Example Source: https://github.com/wordpress/wordpress-coding-standards/blob/develop/README.md Example of running the phpcs command line tool with the WordPress standard. ```bash vendor/bin/phpcs --standard=WordPress wp-load.php ``` -------------------------------- ### Install latest CodeSniffer using PEAR Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to install the latest PHP_CodeSniffer using PEAR. This may fail initially. ```bash C:\xampp\php>pear install --alldeps PHP_CodeSniffer No releases available for package "pear.php.net/PHP_CodeSniffer" install failed ``` -------------------------------- ### Nonce Verification Example Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Fixing-errors-for-input-data Example of how to verify a nonce when processing form data in WordPress. ```php if ( isset( $_POST['foo'], $_POST['foo_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['foo_nonce'] ), 'foo_action' ) ) { $foo = sanitize_text_field( wp_unslash( $_POST['foo'] ) ); // ... ``` -------------------------------- ### Upgrade all PEAR packages Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to upgrade all installed PEAR packages to their latest versions. ```bash C:\xampp\php>pear upgrade-all ``` -------------------------------- ### PHPCompatibilityWP Custom Ruleset Example Source: https://github.com/wordpress/wordpress-coding-standards/blob/develop/README.md An example of how to configure PHPCompatibilityWP as a custom ruleset in PHP_CodeSniffer. ```xml \\*.php$ ``` -------------------------------- ### Example of using the 'callback' parameter with a switch statement Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Upgrade-Guide-to-WordPressCS-3.0.0-for-Developers-of-external-standards This code snippet demonstrates how to handle different groups within the callback method by using a switch statement on the '$group' parameter. ```php public function callback( $key, $val, $line, $group ) { switch( $group ) { case 'groupA': return $this->callback_for_group_A($key, val, $line); case 'groupB': return $this->callback_for_group_B($key, val, $line); } } ``` -------------------------------- ### Composer global install upgrade Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Upgrade-Guide-to-WordPressCS-3.0.0-for-ruleset-maintainers Commands to upgrade WordPressCS to version 3.0.0 for a global Composer installation. ```bash composer global remove --dev dealerdirect/phpcodesniffer-composer-installer higidi/composer-phpcodesniffer-standards-plugin squizlabs/PHP_codesniffer composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true composer global require --dev wp-coding-standards/wpcs:"^3.0.0" --update-with-dependencies ``` -------------------------------- ### Composer project-based install upgrade Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Upgrade-Guide-to-WordPressCS-3.0.0-for-ruleset-maintainers Commands to upgrade WordPressCS to version 3.0.0 for a project using Composer. ```bash composer remove --dev dealerdirect/phpcodesniffer-composer-installer higidi/composer-phpcodesniffer-standards-plugin squizlabs/PHP_codesniffer composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true composer require --dev wp-coding-standards/wpcs:"^3.0.0" --update-with-dependencies ``` -------------------------------- ### Composer Global Installation Source: https://github.com/wordpress/wordpress-coding-standards/blob/develop/README.md Installs WordPressCS globally for use across multiple projects. ```bash composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true composer global require --dev wp-coding-standards/wpcs:"^3.0" ``` -------------------------------- ### List installed coding standards Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to view the coding standards currently recognized by PHP CodeSniffer. ```bash C:\xampp\php>phpcs -i The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend ``` -------------------------------- ### PHPCS Output Example Source: https://github.com/wordpress/wordpress-coding-standards/blob/develop/README.md Example output from running phpcs on a file, showing errors and warnings. ```text -------------------------------------------------------------------------------- FOUND 6 ERRORS AND 4 WARNINGS AFFECTING 5 LINES -------------------------------------------------------------------------------- 36 | WARNING | error_reporting() can lead to full path disclosure. 36 | WARNING | error_reporting() found. Changing configuration values at | | runtime is strongly discouraged. 52 | WARNING | Silencing errors is strongly discouraged. Use proper error | | checking instead. Found: @file_exists( dirname(... 52 | WARNING | Silencing errors is strongly discouraged. Use proper error | | checking instead. Found: @file_exists( dirname(... 75 | ERROR | Overriding WordPress globals is prohibited. Found assignment | | to $path 78 | ERROR | Detected usage of a possibly undefined superglobal array | | index: $_SERVER['REQUEST_URI']. Use isset() or empty() to | | check the index exists before using it 78 | ERROR | $_SERVER['REQUEST_URI'] not unslashed before sanitization. Use | | wp_unslash() or similar 78 | ERROR | Detected usage of a non-sanitized input variable: | | $_SERVER['REQUEST_URI'] 104 | ERROR | All output should be run through an escaping function (see the | | Security sections in the WordPress Developer Handbooks), found | | '$die'. 104 | ERROR | All output should be run through an escaping function (see the | | Security sections in the WordPress Developer Handbooks), found | | '__'. -------------------------------------------------------------------------------- ``` -------------------------------- ### Using WordPressCS Install Source: https://github.com/wordpress/wordpress-coding-standards/blob/develop/README.md Executes PHP_CodeSniffer with the WordPress standard. ```bash # Project local install vendor/bin/phpcs -ps . --standard=WordPress # Global install %USER_DIRECTORY%/Composer/vendor/bin/phpcs -ps . --standard=WordPress ``` -------------------------------- ### Custom Test Case Example (Not Working - Namespace) Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties PHP example demonstrating a custom test case that is not automatically recognized due to namespace usage. ```php namespace Foo; use My\Plugin\Integrations\TestCase; class MySpecificTest extends TestCase { ``` -------------------------------- ### Custom Test Case Example (Not Working) Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties PHP example demonstrating a custom test case that is not automatically recognized due to namespace usage. ```php use My\Plugin\Integrations\MyTestCase; class MySpecificTest extends MyTestCase { ``` -------------------------------- ### Custom Test Case Example (Working) Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties PHP example demonstrating the correct way to extend a custom test case class. ```php class MySpecificTest extends My\Plugin\Integrations\TestCase { ``` -------------------------------- ### Retry installing latest CodeSniffer Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to retry installing PHP_CodeSniffer after clearing the cache. ```bash C:\xampp\php>pear install --alldeps PHP_CodeSniffer WARNING: channel "pear.php.net" has updated its protocols, use "pear channel-update pear.php.net" to update Package "pear.php.net/PHP_CodeSniffer" dependency "pear.phpunit.de/PHP_Timer" has no releases downloading PHP_CodeSniffer-1.5.5.tgz ... Starting to download PHP_CodeSniffer-1.5.5.tgz (412,025 bytes) ................................................................done: 412,025 bytes install ok: channel://pear.php.net/PHP_CodeSniffer-1.5.5 ``` -------------------------------- ### Setting prefixes from the command line Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of setting the 'prefixes' property from the command line. ```bash phpcs -p . --standard=WordPress --runtime-set prefixes my_prefix,tgmpa ``` -------------------------------- ### Clear PEAR cache Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to clear the PEAR cache, often necessary after installation failures. ```bash C:\xampp\php>pear clear-cache reading directory C:\Users\user\AppData\Local\Temp\pear\cache 10 cache entries cleared ``` -------------------------------- ### Composer Project-based Installation Source: https://github.com/wordpress/wordpress-coding-standards/blob/develop/README.md Installs WordPressCS as a development dependency within a project using Composer. ```bash composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true composer require --dev wp-coding-standards/wpcs:"^3.0" ``` -------------------------------- ### Configure PHPCS installed paths Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to set the path for PHP CodeSniffer to find the newly installed WordPress coding standards. ```bash C:\xampp\php>phpcs --config-set installed_paths c:\xampp\php\pear\PHP\CodeSniffer\Standards\WordPress-Coding-Standards ``` -------------------------------- ### Enable Blank Line Check in Control Structures Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of how to enable the blank line check at the start and end of control structures for the WordPress.WhiteSpace.ControlStructureSpacing sniff. ```xml ``` -------------------------------- ### Verify installed standards Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to check if PHP CodeSniffer now recognizes the WordPress coding standards. ```bash C:\xampp\php>phpcs -i The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Extra and WordPress-VIP ``` -------------------------------- ### Namespace Change - Old Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Upgrade-Guide-to-WordPressCS-2.0.0-for-Developers-of-external-standards Illustrates the old namespace usage for extending WordPressCS sniffs. ```php use WordPress\Sniff; class MyCustomSniff extends Sniff {} ``` -------------------------------- ### Setting text_domain from the command line Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of setting the 'text_domain' property from the command line. ```bash phpcs -p . --standard=WordPress --runtime-set text_domain my-slug,default ``` -------------------------------- ### Setting prefixes from the command line Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties This command line example demonstrates how to set the 'prefix' property for the WordPress.NamingConventions.PrefixAllGlobals sniff. ```bash vendor/bin/phpcs --config-set WordPress.NamingConventions.PrefixAllGlobals.prefix "my_" ``` -------------------------------- ### Namespace Change - New Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Upgrade-Guide-to-WordPressCS-2.0.0-for-Developers-of-external-standards Illustrates the new namespace usage for extending WordPressCS sniffs after the change to WordPressCS\WordPress. ```php use WordPressCS\WordPress\Sniff; class MyCustomSniff extends Sniff {} ``` -------------------------------- ### Configuring WordPress.WhiteSpace.ControlStructureSpacing for closure whitespace Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of setting the 'spaces_before_closure_open_paren' property to '0' for the WordPress.WhiteSpace.ControlStructureSpacing sniff. ```xml ``` -------------------------------- ### Travis CI Configuration Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Running-in-Travis This YAML script configures Travis CI to install dependencies and run PHPCS for WordPress coding standards. ```yaml language: php jobs: include: # Arbitrary PHP version to run the sniffs against. - php: 8.1 env: SNIFF=1 cache: directories: - $HOME/.composer/cache before_install: # Turn off Xdebug for performance reasons. - phpenv config-rm xdebug.ini || echo 'No xdebug config.' install: # Install WordPressCS via Composer. - if [[ "$SNIFF" == "1" ]]; then travis_retry composer install --no-interaction; fi - if [[ "$SNIFF" == "1" ]]; then phpenv rehash; fi script: # Run against WordPress Coding Standards. # You can use any of the normal PHPCS command line arguments in the command: # https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage - if [[ "$SNIFF" == "1" ]]; then vendor/bin/phpcs -p . --standard=WordPress; fi ``` -------------------------------- ### SublimeLinter Settings - User for PHPCS Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Setting-up-WordPressCS-to-work-in-Sublime-Text Example of user settings for SublimeLinter's PHPCS linter, specifying multiple standards, runtime arguments, and directories to exclude. ```json // SublimeLinter Settings - User { "linters": { "phpcs": { "args": "--standard=WordPress-Extra,WordPress-Docs,PHPCompatibilityWP --runtime-set testVersion 7.2- -s", "excludes": "vendor/" } } } ``` -------------------------------- ### WordPress.NamingConventions.PrefixAllGlobals: prefix everything in the global namespace Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of setting the 'prefixes' property for the WordPress.NamingConventions.PrefixAllGlobals sniff in an XML ruleset. ```xml ``` -------------------------------- ### Custom Nonce Verification Functions Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of how to configure the WordPress.Security.NonceVerification sniff to recognize custom nonce verification functions. ```xml ``` -------------------------------- ### Replacing text domains with WordPress.Utils.I18nTextDomainFixer Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of configuring the WordPress.Utils.I18nTextDomainFixer sniff to replace old text domains with a new one. ```xml ``` -------------------------------- ### Custom printing functions configuration Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of how to configure custom printing functions for the `WordPress.Security.EscapeOutput` sniff. ```xml ``` -------------------------------- ### Custom escaping and auto-escaped functions configuration Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of how to configure custom escaping and auto-escaped functions for the `WordPress.Security.EscapeOutput` sniff. ```xml ``` -------------------------------- ### Setting text_domain from the command line (WordPressCS 0.11.0+) Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties This command line example shows how to set the 'text_domain' property for the WordPress.WP.I18n sniff. ```bash vendor/bin/phpcs --config-set WordPress.WP.I18n.text_domain "my-text-domain" ``` -------------------------------- ### Configure Space Before Colon in Alternative Syntax Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of how to configure the space before the colon in alternative syntax for control structures in the WordPress.WhiteSpace.ControlStructureSpacing sniff. ```xml ``` -------------------------------- ### WordPress.WP.I18n: setting your text domain Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of setting the 'text_domain' property for the WordPress.WP.I18n sniff in an XML ruleset. ```xml ``` -------------------------------- ### Sniff::phpcsCommentTokens Property - New Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Upgrade-Guide-to-WordPressCS-2.0.0-for-Developers-of-external-standards Demonstrates the updated approach using PHPCS native property and Tokens::$phpcsCommentTokens. ```php use WordPress\Sniff; use PHP_CodeSniffer\Util\Tokens; class MyCustomSniff extends Sniff { public function process_token( $stackPtr ) { if ( isset( Tokens::$phpcsCommentTokens[ $this->tokens[ $stackPtr ]['code'] ] ) ) { // Do something. } } } ``` -------------------------------- ### Original code with undefined array index error Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Fixing-errors-for-input-data An example of code that might trigger an 'undefined superglobal array index' error. ```php if ( 'Yes' === $_POST['auth_step'] ) { // ... ``` -------------------------------- ### Setting minimum supported WP version for all sniffs in one go (WordPressCS 0.14.0+) Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties This command line example shows how to set the 'minimum_supported_wp_version' property for all sniffs at once. ```bash vendor/bin/phpcs --config-set WordPress.Core.MinimumSupportedWPVersion.minimum_supported_wp_version "5.0.0" ``` -------------------------------- ### Configuring auto-escaped functions Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Ignoring-Parts-of-a-File An XML configuration example to add a custom function to the list of auto-escaped functions, allowing WordPressCS to suppress errors automatically. ```xml ``` -------------------------------- ### Ignoring specific tokens for Precision Alignment Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of how to use the 'ignoreAlignmentTokens' property to exempt certain tokens from precision alignment checks in the WordPress.WhiteSpace.PrecisionAlignment sniff. ```xml ``` -------------------------------- ### Sniff::phpcsCommentTokens Property - Old Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Upgrade-Guide-to-WordPressCS-2.0.0-for-Developers-of-external-standards Shows an example of using the removed Sniff::$phpcsCommentTokens property. ```php use WordPress\Sniff; class MyCustomSniff extends Sniff { public function process_token( $stackPtr ) { if ( isset( $this->phpcsCommentTokens[ $this->tokens[ $stackPtr ]['type'] ] ) ) { // Do something. } } } ``` -------------------------------- ### Handling Incorrectly Passed Array Properties - Old Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Upgrade-Guide-to-WordPressCS-2.0.0-for-Developers-of-external-standards Example of using the merge_custom_array method with incorrectly passed array properties. ```php use WordPress\Sniff; class MyCustomSniff extends Sniff { public function process_token( $stackPtr ) { $this->property_name = $this->merge_custom_array( $this->property_name, array(), false ); } } ``` -------------------------------- ### Clear PEAR cache again Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to clear the PEAR cache after upgrading packages. ```bash C:\xampp\php>pear clear-cache reading directory C:\Users\user\AppData\Local\Temp\pear\cache 382 cache entries cleared ``` -------------------------------- ### Fetch WordPress coding standards Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to clone the WordPress Coding Standards repository into the CodeSniffer standards directory. ```bash C:\xampp\php\pear\PHP\CodeSniffer\Standards>git clone https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git ``` -------------------------------- ### Update PEAR channel Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to update the pear.php.net channel as prompted. ```bash C:\xampp\php>pear channel-update pear.php.net Updating channel "pear.php.net" Update of Channel "pear.php.net" succeeded ``` -------------------------------- ### Navigate to CodeSniffer standards directory Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to change the current directory to where CodeSniffer standards are located. ```bash C:\xampp\php>cd pear\PHP\CodeSniffer\Standards ``` -------------------------------- ### Updating WordPressCS Source: https://github.com/wordpress/wordpress-coding-standards/blob/develop/README.md Updates an existing WordPressCS installation to the latest version. ```bash # Project local install composer update wp-coding-standards/wpcs --with-dependencies # Global install composer global update wp-coding-standards/wpcs --with-dependencies ``` -------------------------------- ### check_validation_in_scope_only example Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Demonstrates how the `check_validation_in_scope_only` property affects validation checks. ```php // When `check_validation_in_scope_only` is `false`, this is considered valid: if ( ! isset( $_GET['post_id'] ) ) { // Do stuff, like maybe return or exit (but could be anything) } foo( $_GET['post_id'] ); // When `check_validation_in_scope_only` is `true`, the above would be invalid. // In that case, the variable will only be considered validated if used within // the scope of the validating condition, like this: if ( isset( $var ) ) { foo( $var ); } ``` -------------------------------- ### Disabling translator comments check Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of how to disable the 'check_translator_comments' property for the WordPress.WP.I18n sniff. ```xml ``` -------------------------------- ### Setting minimum_wp_version from the command line Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties This bash command shows how to set the `minimum_wp_version` property for all sniffs from the command line using the `--runtime-set` option. ```bash phpcs . --standard=WordPress --runtime-set minimum_wp_version 4.5 ``` -------------------------------- ### Fixing undefined array index using empty() Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Fixing-errors-for-input-data Demonstrates how to check for the existence and non-emptiness of an array key using empty(). ```php if ( ! empty( $_POST['auth_step'] ) && 'Yes' === $_POST['auth_step'] ) { // ... ``` -------------------------------- ### Uninstall old CodeSniffer Source: https://github.com/wordpress/wordpress-coding-standards/wiki/How-to-use-WordPressCS-with-Eclipse-and-XAMPP Command to uninstall the old PHP_CodeSniffer version if present. ```bash C:\xampp\php>pear uninstall PHP_CodeSniffer uninstall ok: channel://pear.php.net/PHP_CodeSniffer-1.5.5 ``` -------------------------------- ### Setting alignMultilineItems property Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of how to set the alignMultilineItems property for the WordPress.Arrays.MultipleStatementAlignment sniff in an XML ruleset. ```xml ``` -------------------------------- ### Setting maxColumn property Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Customizable-sniff-properties Example of how to set the maxColumn property for the WordPress.Arrays.MultipleStatementAlignment sniff in an XML ruleset. ```xml ``` -------------------------------- ### Fixing undefined array index using array_key_exists() Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Fixing-errors-for-input-data Demonstrates how to check for the existence of an array key using array_key_exists(). ```php if ( array_key_exists( 'auth_step', $_POST ) && 'Yes' === $_POST['auth_step'] ) { // ... ``` -------------------------------- ### Fixing undefined array index using isset() Source: https://github.com/wordpress/wordpress-coding-standards/wiki/Fixing-errors-for-input-data Demonstrates how to check for the existence of an array key using isset() before accessing it. ```php if ( isset( $_POST['auth_step'] ) && 'Yes' === $_POST['auth_step'] ) { // ... ```