Enable WPCS In Visual Studio Code

Prerequisites

Composer installed

Installation Steps

1. Install Required Dependencies

Navigate to your project’s root directory and install the following packages using Composer:

# Install PHP_CodeSniffer
composer require --dev squizlabs/php_codesniffer

# Install WordPress Coding Standards
composer require --dev wp-coding-standards/wpcs

# Install the Composer installer for PHP_CodeSniffer Standards
composer require --dev dealerdirect/phpcodesniffer-composer-installer

2. Set Up VS Code Extension

Install the extension valeryanm.vscode-phpsab in your VSC

Important: If you have the PHP Intelephense extension disable it

3. Configure VS Code Settings

  1. Create a .vscode folder in your project root if it doesn’t exist
  2. Create a settings.json file inside the .vscode folder
  3. Add the following configuration:
{
  "phpsab.standard": "phpcs.xml",
  "phpsab.executablePathCBF": "./vendor/bin/phpcbf",
  "phpsab.executablePathCS": "./vendor/bin/phpcs",
  "[php]": {
    "editor.defaultFormatter": "valeryanm.vscode-phpsab",
    "editor.formatOnSave": true,
    "editor.tabSize": 4,
    "editor.insertSpaces": false,
    "phpsab.allowedAutoRulesets": [
      "phpcs.xml",
      "phpcs.xml.dist",
      "WordPress"
    ]
  },
  "phpsab.snifferArguments": [
    "--standard=phpcs.xml",
    "--tab-width=4",
    "-s"
  ],
  "phpsab.fixerArguments": [
    "--standard=phpcs.xml",
    "--tab-width=4",
    "-s"
  ]
}

4. Config for WPCS

  1. In the root project create a file called phpcs.xml
<?xml version="1.0"?>
<ruleset name="WordPress Block Standards">
    <description>WordPress Coding Standards for Blocks</description>

    <!-- Use WordPress as a base -->
    <rule ref="WordPress">
        <!-- Common exclusions for blocks -->
            <!-- File doc comment exclude -->
            <exclude name="Squiz.Commenting.FileComment.Missing"/>
            
            <!-- Output escaping excludes -->
            <exclude name="WordPress.Security.EscapeOutput.OutputNotEscaped"/>
            <exclude name="WordPress.Security.EscapeOutput.UnsafePrintingFunction"/>

    </rule>

    <!-- Force tab indentation -->
    <arg name="tab-width" value="4"/>
    <rule ref="Generic.WhiteSpace.ScopeIndent">
        <properties>
            <property name="indent" value="4"/>
            <property name="tabIndent" value="true"/>
        </properties>
    </rule>
</ruleset>

4. Verify Installation

  1. Open any PHP file in your project
  2. Right-click and select “Format Document”

Troubleshooting

If formatting isn’t working:

  1. Make sure you’re in the correct directory
  2. Verify the packages are installed by running:
./vendor/bin/phpcs --version

Also try to close and open again VSC to refresh after the initial instalation


Done! You are all set to test it in your php file.
Remember this is not a global config for WPCS but for an especific project.

One response

  1. Thanks, worked perfectly, this is like the 6th website that I had to go through to get it working!

    Like

Leave a comment