A brief tutorial for getting WordPress coding standards in your WordPress project, adapted to WPFactory needs.
- Install PHP_CodeSniffer and WordPress Coding Standards for PHP_CodeSniffer composer packages, with
composer install
orcomposer update
. Take a look at our composer.json - Copy phpcs.xml to your project
- Copy the contents from .gitignore to your project .gitignore
Note
If for some reason you get the error "PHPCSUtils" does not exist" please change these libraries on composer.json file:
"squizlabs/php_codesniffer": "^3.7.1",
"wp-coding-standards/wpcs": "^2.3.0"
- Copy .github/workflows/php.yml to you project if you want to get coding standards reports from github on each push
- Install PHP Sniffer & Beautifier for VS Code extension
- Copy the .vscode folder to your project
😎 Note: This setup allows to sniff the code and to reformat it automatically on file saving
- Install SublimeLinter package
- Install SublimeLinter-phpcs package
- Setup SublimeLinter
Access Preference > Package settings > SublimeLinter > Settings and config the linters
node like this:
"linters": {
"phpcs": {
"disable": false,
"SublimeLinter.linters.phpcs.args": "--standard='${folder}/phpcs.xml'",
"executable": "${folder}/vendor/squizlabs/php_codesniffer/bin/phpcs.bat",
},
😅 Note: This setup does not allow to fix code errors automatically on file saving yet, you can only sniff the code.
- Install NppExec plugin
- Setup NppExec
- Access Plugins > NppExec > Execute, create this command and save it:
$(CURRENT_DIRECTORY)/vendor/squizlabs/php_codesniffer/bin/phpcbf.bat --standard=phpcs.xml $(FULL_CURRENT_PATH)
NPP_SENDMSG NPPM_RELOADFILE 0 "$(FULL_CURRENT_PATH)"
- Access Settings > Shortcuts mapper > Plugin commands
- Find the saved command looking for the last line of NppExec.dll
- Assign a shortcut to it (Probably Alt + Shift + F)
- Be happy
- You might want to enable Settings > Preferences > Misc > Update silently
😅 Note: This setup does not allow to sniff the code, you can only reformat it.