Istiaq Nirab
Posted on December 20, 2022
First of all you need to install wpcs dependencies on your plugin via composer :
composer require --dev wp-coding-standards/wpcs:dev-develop
After install this package, you'll get access these two types of commands :
# check php coding standard releted issues
./vendor/bin/phpcs
# fix coding standard automatically
./vendor/bin/phpcbf
I recommend to register these commands with composer scripts.
"scripts": {
"phpcs": "\"vendor/bin/phpcs\"",
"phpcbf": "\"vendor/bin/phpcbf\"",
"sniffs": "\"vendor/bin/phpcs\" -e",
}
then you can easily access phpcs commands via composer:
composer phpcs
composer phpcbf
composer sniffs
Extensions
Some vsCode extensions, I always use :
I recommend PHP Intelephense for PHP built-in functions completion, code completion (IntelliSense), go to definition support and many more.
phpcs extension allow us to display phpcs related issues without running phpcs commands every time & also fix coding styles after save file.
To enable WordPress coding standards:
"phpsab.standard": "WordPress",
It's automatically configure setup from your root project composer.json.
But what happen if you want to develop something like WooCommerce Extension OR any other plugin based plugin !!
You need to open wordpress plugins directory instead of opening single plugin directory.
Then you can get better code completion (IntelliSense) support.
But the phpcs extension doesn't work, you need to configure locally in the plugins directory. So, create new folder called .vscode under the project root and create settings.json file here.
Inside settings.json :
{
"phpsab.executablePathCS": "./subscription-pro/vendor/bin/phpcs",
"phpsab.executablePathCBF": "./subscription-pro/vendor/bin/phpcbf",
"phpsab.standard": "WordPress",
"[php]": {
"editor.defaultFormatter": "valeryanm.vscode-phpsab"
}
}
Now, you can see phpcs working fine.
The last extension is WordPress Hooks IntelliSense, which will provide better autocompletion for hooks.
Thanks.
Posted on December 20, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.