Skip to content

Instantly share code, notes, and snippets.

@ajvillegas
Created July 30, 2021 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ajvillegas/9f16240bf6149c26f1067e17a33d0997 to your computer and use it in GitHub Desktop.
Save ajvillegas/9f16240bf6149c26f1067e17a33d0997 to your computer and use it in GitHub Desktop.

Setting Up WordPress Coding Standards with VSCode

Install WordPress Coding Standards (WPCS)

First, make sure you have Composer installed in your system.

In order to use the WPCS you'll need the PHP_CodeSniffer (PHPCS) and the WPCS rules (sniffs) installed.

You can install PHP_CodeSniffer globally using Composer by running this command:

composer global require "squizlabs/php_codesniffer=*"

Make sure you have the composer bin dir in your PATH. The default value is ~/.composer/vendor/bin/.

Follow this link for more detailed installation instructions.

Once you have PHP_CodeSniffer installed globally, you'll be able to run the phpcs and the phpcbf commands from your terminal or bash window.

Now install the WordPress Coding Standards rules by running the following commands:

# Clone WPCS repo.
git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs

# Set phpcs configuration for wpcs.
phpcs --config-set installed_paths /path/to/wpcs

Now go to VSCode, install phpcs extension and then go to settings.json, and add the following settings. Finally reload.

// Setting for phpcs.
"phpcs.executablePath": "/path/to/.composer/vendor/bin/phpcs",
"phpcs.standard": "WordPress",

Check the available standards phpcs -i

PHP Extensions & VSCode Settings to Automate the WPCS Workflow

Recommended Extensions

VSCode Settings

{
    // PHP/WORDPRESS
    "php.suggest.basic": false,
    "intelephense.formatProvider.enable": false,
    "[php]": {
        "editor.formatOnSave": false,
        "editor.formatOnPaste": false
    },
    // PHPCS.
    "phpcs.executablePath": "/path/to/.composer/vendor/bin/phpcs",
    "phpcs.standard": "WordPress",
    // PHPCBF.
    "phpcbf.onsave": true,
    "phpcbf.standard": "WordPress",
    "phpcbf.executablePath": "/path/to/.composer/vendor/bin/phpcbf",
    // PHP Files.
    "files.associations": {
        ".php_cs": "php",
        ".php_cs.dist": "php",
        // Super important, otherwise intelephense will not auto-index your project.
        "*.php": "php"
    },
    "phpcs.ignorePatterns": ["*/vendor/*", "*/vendors/*"],
    // Save and Run.
    "saveAndRun": {
        "commands": [
            {
                "match": "\\.php$",
                "cmd": "phpcbf -q --standard='WordPress' ${file}",
                "silent": true
            }
        ]
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment