vsCode Configuration for WordPress Plugin

ok9xnirab

Istiaq Nirab

Posted on December 20, 2022

vsCode Configuration for WordPress Plugin

First of all you need to install wpcs dependencies on your plugin via composer :

composer require --dev wp-coding-standards/wpcs:dev-develop
Enter fullscreen mode Exit fullscreen mode

After install this package, you'll get access these two types of commands :

# check php coding standard releted issues

./vendor/bin/phpcs
Enter fullscreen mode Exit fullscreen mode
# fix coding standard automatically

./vendor/bin/phpcbf
Enter fullscreen mode Exit fullscreen mode

I recommend to register these commands with composer scripts.

"scripts": {
    "phpcs": "\"vendor/bin/phpcs\"",
    "phpcbf": "\"vendor/bin/phpcbf\"",
    "sniffs": "\"vendor/bin/phpcs\" -e",
}
Enter fullscreen mode Exit fullscreen mode

then you can easily access phpcs commands via composer:

composer phpcs
composer phpcbf
composer sniffs
Enter fullscreen mode Exit fullscreen mode

Extensions

Some vsCode extensions, I always use :

PHP-Intelephense

I recommend PHP Intelephense for PHP built-in functions completion, code completion (IntelliSense), go to definition support and many more.

Phpcs

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",
Enter fullscreen mode Exit fullscreen mode

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.

vsCode explorer

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"
  }
}
Enter fullscreen mode Exit fullscreen mode

Now, you can see phpcs working fine.

vscode phpcs

The last extension is WordPress Hooks IntelliSense, which will provide better autocompletion for hooks.

wordpress intellisense

Thanks.

💖 💪 🙅 🚩
ok9xnirab
Istiaq Nirab

Posted on December 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related