I am slowly moving away from Sublime Text to Microsoft’s Visual Studio Code due to VS Code’s superior feature-set like a built-in file navigation tree (which is better than ST’s SideBarEnhancements package), IntelliSense and built-in Git.
Below are the steps to have VS Code automatically sniff and display warnings/errors (if any) in your PHP files per WordPress Coding Standards.
Step 1
Step 2
In the system terminal (navigate to your home directory, if you are elsewhere) run
composer create-project wp-coding-standards/wpcs --no-dev
Running this command will
- install WordPress standards into ~/wpcs directory.
- install PHP_CodeSniffer.
- register WordPress standards in PHP_CodeSniffer configuration.
- make
phpcs
command available from ~/wpcs/vendor/bin.
Step 3
Mac
Run
ln -s ~/wpcs/vendor/bin/phpcs /usr/local/bin/phpcs
This will create /usr/local/bin/phpcs
as an alias or symbolic link to ~/wpcs/vendor/bin/phpcs
where ~
is your home directory (Ex.: /Users/sridharkatakam
).
Windows
Go to Control Panel. Search for path
and click on “Edit the system environment variables”.
Click on “Environment Variables…” in the System Properties’ Advanced tab.
In the bottom “System variables” section, select Path
and click Edit.
Add a new entry having this path:
C:\Users\Admin\wpcs\vendor\bin
where Admin
is your current admin-level username.
Step 4
In VS Code, go to Extensions, search for phpcs
and install it.
Bring up your user settings by pressing ⌘, and add
"phpcs.standard": "WordPress"
Step 5
Restart VS Code.
That’s it.
Note:
- To check the value of $PATH environment in a terminal run
echo $PATH
. - To add a directory to $PATH, run
open ~/.bash_profile
. Then in the text editor addexport PATH="/Users/sridharkatakam/wpcs/vendor/bin:$PATH"
where/Users/sridharkatakam/wpcs/vendor/bin
is the directory whose path is to be added.
Why not use Brackets?
So, I am giving it a try and it looks interestign… Had a few install problems.
When I got to the symbolic link step iTerm would error saying /usr/local/bin/phpcs existed.
When I opened VC it could not find phpcs.
I add the path in bash but still no go.
So, here is what ended up working for me:
mv /usr/local/bin/phpcs /usr/local/bin/phpcs.old
Then:
ln -s ~/wpcs/vendor/bin/phpcs /usr/local/bin/phpcs
Seems odd, but it worked.
mv /usr/local/bin/phpcs /usr/local/bin/phpcs.old
Hi Sridhar,
Thank you for your very helpful article. You managed to get me passed the “Cannot locate” message which has plagued me for months – so grateful.
Now I get “phpcs: Cannot read property ‘length’ of null”. Any ideas what this might mean? Google search returns nothing.
Thanks
Joe
Did you resolve this issue please? Your comment is the only result on google!
You need to install the phpcs extension by Ioannis Kappas v0.7.0
I had this error because I had both extensions phpcs by Ioannis Kappas v0.7.0 and phpcs by Adam Charron v0.8.1 installed at the same time.
Resolved it by uninstalling both extensions and reinstalling phpcs 0.7.0 and reloaded the editor.
Hope it helps!
Hello,
Thanks!
How can I format / indent the code in WordPress standards?
Follow https://sridharkatakam.com/linting-and-formatting-in-visual-studio-code-for-wordpress/
[…] the past, I wrote about setting up WordPress PHP code standards in Visual Studio […]
[…] How to Set Up WordPress Coding Standards in Visual Studio Code […]
[…] For Mac, you can follow Jason’s excellent tutorial. For Windows users, there’s a good explanation here. And for Linux users, there’s this guide by Tom […]