Note: Here’s a more updated and simple guide.

Adhering to a certain set of coding standards is important when it comes to writing good quality code. It makes your code easier to maintain and read not only for yourself but other developers as well.

WordPress has its own set of coding standards and it is something you should to follow if you plan to contribute to Core, plugins or themes. Even if you are not, it’s still a good way to level up as a Developer.

Before we look at the steps, I’m going to assume you already have Composer installed. If not, here’s the official documentation on how to get it up and running.

Install PHP CodeSniffer (PHPCS)

composer global require "squizlabs/php_codesniffer=*"

PHPCS is a development tool that detects violations of coding standard and automatically corrects them. Do note that PHP 5.4 or greater is required.

Download WPCS

 git clone -b main https://github.com/WordPress/WordPress-Coding-Standards.git wpcs

cd to your desired directory and run the above command in your terminal. This will download WPCS into a folder called wpcs.

Personally, I’d create a Utilities folder and place coding standards, custom rulesets etc. inside.

Add WPCS to PHPCS

/path/to/composer/vendor/bin/phpcs --config-set installed_paths /path/to/WPCS, /path/to/another-standards

Now that we have WPCS and PHPCS in our system, we can run the above command and the latter know where our coding standard is located.

If you are getting the command not found: phpcs message, ensure that your path to PHPCS is correct. Since we installed it globally, the path should be something like /users/your_user_name/.composer/vendor/bin/phpcs

Also, do note that the installed_paths command overwrites any previously set installed_paths. If you have existing coding standards, please remember to include their paths together with whatever coding standards you are adding separated by a comma.

Check to ensure WPCS is added

/path/to/composer/vendor/bin/phpcs -i

If WPCS is added correctly, we should see the following output:

The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1, PSR12, WordPress, WordPress-Extra, WordPress-Docs and WordPress-Core

Congrats! We are done with the complicated part ๐ŸŽ‰๐ŸŽŠ. What’s left now is to configure VS Code!

Configuring VSCode

  1. Install phpcs and phpcbf extensions
phpcs extension
phpcbf extension

The phpcs extension enables linting for all “PHP files in our editor while phpcbf will try to beautify and fix our code according to the chosen coding standard.

  1. Configure settings.json

Once those two are installed, open up the editor settings under Code > Preferences > Settings. Toggle to the JSON view and add the following values:

"phpcs.enable": true,
"phpcs.executablePath": "/path/to/composer/vendor/bin/phpcs",
"phpcs.standard": "WordPress",
"phpcbf.enable": true,
"phpcbf.documentFormattingProvider": true,
"phpcbf.onsave": true,
"phpcbf.executablePath": "/path/to/composer/vendor/bin/phpcbf",
"phpcbf.standard": "WordPress",

Save the settings.json and restart VSCode.

  1. Test!

If everything is configured correctly, VSCode should start linting your PHP code. As seen in the above demo, our initial code violated the following rules:

  1. No spacing inside parenthesis
  2. Missing doc comment for function
  3. Not using Yoda conditions
  4. Not escaping output

Since we configured phpcbf.onsave: true, VSCode should auto fix some issues when we save the file (in our e.g., it fixed #1). If, for some strange reason, the auto save fails to work, try right click > Format Document.

As for the other issues, we can mouse over the underlined code to get more information and then fix them manually.

And that’s it. From now on, you should feel more confident about the quality of your code! ๐Ÿ’ช

Additional resources:

34 responses to “Setting up WordPress Coding Standards in VS Code”

  1. This was quite helpful, thanks.

    I shared a summary of the same here: https://wpdevdesign.com/wp-coding-standards-in-vscode/.

    1. Thanks Sridhar! Glad it was helpful!

  2. Hey Edmund,

    With this setup files aren’t getting saved and shows this message at the bottom right:

    > Saving ‘plugin.php’: Running ‘phpcbf’ Formatter (configure).

    Any idea?

    1. Hmm that’s a strange one. Let me see if I can replicate that issue. Will keep you posted!

      1. Hey Edmund,

        any Updates? i have the same issue

        1. Hello Dennis,

          Hmm it’s working fine for me. Let me try and setup the thing again and see if it works.

          In the meantime can you try and see if this works – https://stackoverflow.com/a/62777462

      1. Hello Djs,

        can u try and see if this works -https://stackoverflow.com/a/62777462

  3. How do you do the auto complete? E.g. how did you get it to automatically create the doc comment?

    1. Hey George!

      I believe that was copied and pasted from another file ๐Ÿ˜… Just wanna keep that recording short.

  4. Thank you so much. I spent days doing it manually.

  5. You are most welcome!

  6. Thank you.

    My settings.json file looks like this:

    “`json
    {
    “editor.insertSpaces”: false,
    “editor.wordWrap”: “on”,
    “prettier.useTabs”: true,
    “prettier.singleQuote”: true,
    “editor.formatOnSave”: false,
    “phpcs.enable”: true,
    “phpcs.executablePath”: “C:/Users/boxof/AppData/Roaming/Composer/vendor/bin/phpcs”,
    “phpcs.standard”: “WordPress”,
    “phpcbf.enable”: true,
    “phpcbf.executablePath”: “C:/Users/boxof/AppData/Roaming/Composer/vendor/bin/phpcbf.bat”,
    “phpcbf.documentFormattingProvider”: true,
    “phpcbf.onsave”: false,
    “phpcbf.standard”: “WordPress”,
    }
    “`

  7. After I followed your instructions, I got this error: phpcs: Referenced sniff “WordPress.VIP” does not exist
    Can you please check and let me know the solution?

  8. Hello Yin!

    It seems like your project has its own custom ruleset which includes WordPress.VIP. Could you check if your project folder (usually in the theme) includes either a phpcs.xml or phpcs.xml.dist file? If so, try removing it temporarily and see if it works. Custom rulesets will overwrite what is set in your VSCode settings.

    If it doesnt work could you show me your VSCode settings?

  9. Hi,
    I’m getting error :
    PHPCS: WordPress coding standard is not installed.

    1. Hey Ravi!

      It sounds like your PHPCS doesn’t know where the WPCS is located in your system (Step 3 – Add WPCS to PHPCS). Do make sure that the path to the WPCS folder is correct.

      If everything is configured properly u should see WPCS installed when you run /path/to/composer/vendor/bin/phpcs -i.

      Ed

  10. Nice article Edmund.

    To fetch old phpcs config please execute this command:
    .config/composer/vendor/bin/phpcs –config-show

    Which list out older config paths.
    Therefore you can decide whichever older paths are need to be maintained then you can update the config command with new and old config set.

    With old coding standards:
    .config/composer/vendor/bin/phpcs –config-set installed_paths ~/code/wpcs,/home/username/oldcs1

    Ignore and override:
    .config/composer/vendor/bin/phpcs –config-set installed_paths ~/code/wpcs

    Unfortunately there is no append command therefore whenever updating you have to manually do this.

  11. That’s an awesome tip Vijayan. Thanks for sharing!

    1. Indeed, Edmund thanks for the appreciation.

  12. very helpful, thanks for the article!

  13. Hi,
    I have problem with the path [windows 10, vs code]
    PHPCBF: spawn C:/Users/Hayder/AppData/Roaming/Composer/vendor/bin/phpcbf ENOENT. executablePath not found.

  14. Hi
    Thanks for an easy to follow article. I’m new convert from Brackets to VScode so please forgive me if there is an obvious fix to this.
    After following your article, when I now open some WP PHP in VScode it highlights a number of errors. For example, two of many are “Expected 1 spaces before closing parenthesis; 0 found phpcs” in my function calls and “Tabs must be used to indent lines; spaces are not allowed phpcs” at the start of lines.
    Both are easily fixed, which removes the red squiggly line and the report of an error.
    When I Format Document, either manually or upon file save, the changes I’ve made to resolve the two example issues are removed and the red squiggly lines (and error reports) return.
    It seems as though the formatter is changing my code and breaking it’s own rules.
    Everything seems to be set up OK. phpcbf -i returns
    The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs and WordPress-Extra
    Any help would be greatly appreciated.

    experience a problem when using the

    1. Hello Phil!

      Sorry for the late response! It sounds like the default PHP formatter for your editor is not configured correctly. Here’s what I have in my VSCode settings:

      "[php]": {
      "editor.defaultFormatter": "persoderlind.vscode-phpcbf"
      }

      Can you try and see if it helps?

      Ed

  15. Just wanted to say thanks for this walkthrough. I helped me a ton.

    1. I’m glad you found it useful!

  16. Thank you! This helped me lot.

    The config.json is missing a comma behind
    “phpcs.standard”: “WordPress”

    1. Updated! Thanks for pointing this out ๐Ÿ™

  17. Hello. Can i use full way to path in setting json “c:\Users\username\AppData\Roaming\Composer\vendor\bin\”?

  18. Hello, Chan

    Could you please make a video tutorial? I find it confusing at the end.

  19. Hello Edmund,
    “Is there any configuration so that the echo text will also warn me when the translation is missing? Also, where can I find the ruleset for WPCS and PHPCS?”

    Would you like me to provide more information on the ruleset for WPCS and PHPCS?

    Thank you.

  20. Step 2 now has a bug because the `master` branch is now called `main`:

    git clone -b main https://github.com/WordPress/WordPress-Coding-Standards.git wpcs

    1. Updated. Thank you for catching this ๐Ÿ™!

  21. AMAZING , Exactly what I was looking for, thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *