Setup PHP CodeSniffer (WordPress) In Visual Studio Code

This guide will help you set up PHP CodeSniffer (PHPCS) in Visual Studio Code to enforce WordPress Coding Standards.

Prerequisites

  • PHP 7.4 or higher
  • Composer
  • Visual Studio Code
  • VS Code PHP extension

Installation Steps

  1. Install PHP CodeSniffer and WordPress Coding Standards via Composer:
composer require --dev wp-coding-standards/wpcs
  1. Configure PHPCS installed standards path:

Normally, PHPCS will automatically detect the installed standards, but if you have installed them in a custom location, you need to set it manually:

./vendor/bin/phpcs --config-set installed_paths vendor/phpcsstandards/phpcsextra,vendor/phpcsstandards/phpcsutils,vendor/wp-coding-standards/wpcs

Check installed coding standards

> ./vendor/bin/phpcs -i
The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, Modernize, NormalizedArrays, Universal, PHPCSUtils, WordPress, WordPress-Core, WordPress-Docs and WordPress-Extra
  1. Install VS Code Extensions:
    • PHP CodeSniffer (obliviousharmony)
    • PHP Intelephense (optional, but recommended)

VS Code Configuration

  1. Create or update .vscode/settings.json:
{
  "[php]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "obliviousharmony.vscode-php-codesniffer"
  },

   "phpCodeSniffer.standard": "Custom",
   "phpCodeSniffer.standardCustom": "WordPress",
   "phpCodeSniffer.exec.osx": "vendor/bin/phpcs",
   "phpCodeSniffer.exec.linux": "vendor/bin/phpcs"
}
  1. Add .editorconfig file (already included in this project) for consistent coding style across editors.
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

# WordPress Coding Standards
# https://make.wordpress.org/core/handbook/coding-standards/

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
indent_size = 2

[*.{yml,yaml}]
indent_style = space
indent_size = 2

Usage

  • PHPCS will automatically check your PHP files for WordPress Coding Standards compliance
  • Errors and warnings will be displayed in VS Code’s Problems panel
  • Many issues can be fixed automatically using the Quick Fix feature (Ctrl+. or Cmd+.)

Common Standards

The setup includes:

  • WordPress Coding Standards
  • PHPCSExtra (additional sniffs)
  • PHPCSUtils (utility functions)

Troubleshooting

If you encounter issues:

  1. Ensure all dependencies are properly installed
  2. Verify the PHPCS executable path in VS Code settings
  3. Try reloading VS Code after configuration changes

Comments

3 responses to “Setup PHP CodeSniffer (WordPress) In Visual Studio Code”

  1. Специальные предложения на серверы HP в честь праздников
    серверы hp купить https://www.kupit-server-hp.ru/ .

  2. Лучшие цены на стоматологию в Минске: всегда у нас!, поспешите записаться.
    Стоматология Минск цены на услуги http://www.total-implant.ru .

  3. Доступные серверы HP Proliant, Гарантия качества при покупке сервера HP Proliant
    серверы hp https://servera-hp-proliant.ru .