The WordPress Coding Standards are a set of rules for formatting code in themes, plugins, and WordPress core. It’s good practice to follow them, but it’s easier to follow them if you use a tool like PHP CodeSniffer to highlight issues as you code. Easiest of all is when the coding standards are integrated into your IDE (i.e., VS Code), and smart enough to fix formatting issues for you.

But when I decided to set up VS Code to use the WordPress coding standards with PHP CodeSniffer I kept running into problems because there doesn’t seem to be an up-to-date guide. I managed to hack my way through it, and eventually I got it running smoothly with just a few steps. Now I can easily set up any project to use the WordPress Coding Standards to help me format my PHP code properly.

One caveat: I use MacOS, so this guide should work for MacOS and Linux users. I’m sure it’s not difficult to adapt for Windows, but since I don’t use Windows I didn’t try to adapt it myself.

1. Install Composer

If you don’t already have Composer installed, start by following the installation guide to install it globally. These are the steps I followed:

  1. Get Composer by running the download script in a terminal window. This checks your PHP settings and then downloads a composer.phar file in the current directory.
  2. Move Composer to a directory in your PATH. I used mv composer.phar /usr/local/bin/composer as recommended in the Composer installation guide.
  3. Make sure it worked by entering composer in the terminal. You should see something that starts like this:
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.10.1 2026-06-04 10:25:59
Usage:
  command [options] [arguments]

2. Install PHP CodeSniffer (phpcs) & the WordPress Coding Standards (WPCS) in your project

With your project open in VS Code, open the integrated terminal (Control (⌃) + ` or select Terminal / New Terminal from the menu). Copy and paste the following into the terminal:

composer -n require "squizlabs/php_codesniffer=^3.13.6"
composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer -n -W require "wp-coding-standards/wpcs=*"

This runs three commands:

  1. composer -n require "squizlabs/php_codesniffer=*"
    Installs PHP CodeSniffer in your project using Composer.
  2. composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
    Allows phpcs to use a plugin to install the WordPress PHP coding standards ruleset.
  3. composer -n -W require "wp-coding-standards/wpcs=*"
    Installs the WordPress coding standards ruleset and updates its dependencies. This should also add the ruleset to the phpcs configuration file.

The -n/--no-interaction option prevents Composer from asking questions. (Otherwise it will ask if you meant to use the require-dev command.)

The -W/--with-all-dependencies option updates the WPCS dependencies and related packages (even if locked), and tells phpcs where to find the new ruleset. (If you get an error message later on that phpcs can’t find the WPCS ruleset, try running ./vendor/bin/phpcs --config-set installed_paths ../../wp-coding-standards/wpcs.)

You should end up with a composer.json file in your project directory that looks something like this:

{
    "require": {
        "squizlabs/php_codesniffer": "*",
        "wp-coding-standards/wpcs": "*"
    },
    "config": {
        "allow-plugins": {
            "dealerdirect/phpcodesniffer-composer-installer": true
        }
    }
}

You’ll also end up with a composer.lock file and a vendor directory for the Composer packages.

Set up VS Code

Install the phpcs and phpcbf extensions for VS Code.

phpcs enables linting (highlighting code formatting issues) your PHP code, and phpcbf will try to “beautify” it based on the WordPress coding standards.

Finally, open the workspace settings JSON file. Click on the command palette in VS Code (or press Command (⌘) + Shift (⇧) + P) and select Preferences: Open Workspace Settings (JSON) (try typing “settings.json” if you can’t find it).

Add the following properties:

{
	"phpcs.enable": true,
	"phpcs.executablePath": "./vendor/bin/phpcs",
	"phpcs.standard": "WordPress",
	"phpcbf.enable": true,
	"phpcbf.documentFormattingProvider": true,
	"phpcbf.onsave": true,
	"phpcbf.executablePath": "./vendor/bin/phpcbf",
	"phpcbf.standard": "WordPress",
}

Save the settings.json file, then close and reopen VS Code.

Using phpcs

If everything worked as expected you should see squiggly red lines under any issues identified by phpcs using the WordPress coding standards ruleset.

When you save the file, phpcbf will “correct” your code. (You can always undo the changes.) If you prefer to trigger code formatting manually, press Option (⌥) + Shift (⇧) + F.

If you want to disable formatting on save, just remove the "phpcbf.onsave": true property from settings.json.

Update .gitignore

You do want to include your composer.lock file in your version control, but not your vendor directory, so add /vendor to your .gitignore file.

Other guides

The commands above incorporate a necessary step from the WPCS readme that is easy to miss—especially because it is not obvious that you need to follow it. (If you don’t, you will get a “phpcs: Referenced sniff ‘PHPCSUtils’ does not exist” error.) The -W option in the command above takes care of this.

Edmund Chan’s guide is the most helpful, but it is outdated and misses that step. So does Tom McFarlin’s guide.

Also, both guides assume you want to install the WPCS ruleset globally, but I wanted to keep it in the VS Code workspace because I don’t want to use the WordPress PHP coding standards when I am working on non-WordPress PHP projects.

I hope this helped you get the WordPress PHP coding standards working in VS Code!

Leave a Reply

Client Login