How to Setup WordPress Coding Standards in VSCode
source link: https://www.pontikis.net/blog/how-to-setup-wordpress-coding-standards-in-vscode
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
WordPress Coding Standards are documented in detail in WordPress Developer Handbook. Fortunately, WordPress offers a Github project, which maintains the WordPress Coding Standards rules and the tools needed to inspect and format the code (PHP Code Sniffer And Beautifier).
On your workstation
If you haven’t already, install Composer. Detailed instructions, you can find in this post.
Then, choose a folder in your workstation to set up the WordPress-Coding-Standards project. In my case, this folder is /data/projects/tools
.
cd /data/projects/tools cd WordPress-Coding-Standards composer install |
With the above process:
- WordPress Coding Standards are installed in the directory
WordPress-Coding-Standards
. - PHP_CodeSniffer is installed.
- WordPress Coding Standards are registered in PHP_CodeSniffer configuration
Regarding (3) composer will notice you with the message:
PHP CodeSniffer Config installed_paths set to ../../../,../../phpcompatibility/php-compatibility,../../phpcsstandards/phpcsdevtools,../../phpcsstandards/phpcsextra,../../phpcsstandards/phpcsutils
VSCode
Install PHP Sniffer & Beautifier extension.
Configure extension
Use VSCode settings.json
to apply the following settings:
"phpsab.executablePathCBF" : "/data/projects/tools/WordPress-Coding-Standards/vendor/squizlabs/php_codesniffer/bin/phpcbf" , "phpsab.executablePathCS" : "/data/projects/tools/WordPress-Coding-Standards/vendor/squizlabs/php_codesniffer/bin/phpcs" , "phpsab.autoRulesetSearch" : false , "phpsab.standard" : "WordPress" , "phpsab.snifferArguments" : [ "--ignore=*/wp-admin/*,*/wp-includes/*" ] |
Remember to replace /data/projects/tools
with your own path.
PHP Code Sniffer will automatically detect coding standard errors in PHP documents.
To fix a document (when it is possible) use Right Click ⟿ Format Document.
Ignore files from PHPCS inspection in VSCode
Use the phpsab.snifferArguments
setting as mentioned above to ignore file patterns using the parameter--ignore
:
Example
"phpsab.snifferArguments" : [ "--ignore=*/path1/*,*/path2/*,*/path3/*" ] |
Reference
Entrepreneur | Full-stack developer | Founder of MediSign Ltd. I have over 15 years of professional experience designing and developing web applications. I am also very experienced in managing (web) projects.
Your comments are welcome!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK