WordPress-Coding-Standards
WordPress-Coding-Standards copied to clipboard
Update Guideline for SublimeText and WPCS
According to Slack Conversation, opening issue to update this guideline.
Instead of a ready to copy paste text I will simply share here each and every step I needed to take and the findings I had, please pick what you think is useful to update the guide.
This setup assumes global usage, not project limited, and uses the first suggested package of the DOC (https://github.com/WordPress/WordPress-Coding-Standards/wiki/Setting-up-WPCS-to-work-in-Sublime-Text#sublime-phpcs-package)
- Follow this to install WPCS and PHPCS since you likely will have Composer 2 and not the outdated Composer 1 on your machine. Thus, start with this command:
composer global require dealerdirect/phpcodesniffer-composer-installer --update-no-dev
composer global require wp-coding-standards/wpcs --update-no-dev
- Put the bin in your path, with following command:
nano ~/.zshrc
, addexport PATH="$HOME/.composer/vendor/bin:$PATH"
to the file and write it out, close the file, thensource ~/.zshrc
and confirm it worked withwhere phpcs
(you should see something like/Users/your_user_name/.composer/vendor/bin/phpcs
) - Point PHPCS to WPCS config:
phpcs --config-set installed_paths /Users/your_user_name/.composer/vendor/wp-coding-standards/wpcs
- In Sublime, open Preferences > Package Control and choose Install Package, then select
Phpcs
package (https://benmatselby.dev/sublime-phpcs/) - In Sublime, open Preferences > Package Settings > PHP Code Sniffer > Settings Default and copy the entire content, then open Preferences > Package Settings > PHP Code Sniffer > Settings User and paste the entire content. Now alter these points:
- item
phpcs_executable_path
should be (full line statement):"phpcs_executable_path": "/Users/your_user_name/.composer/vendor/bin/phpcs",
- item
phpcs_additional_args
should be (full line statement):
"phpcs_additional_args": {
"--standard": "WordPress",
"-n": ""
},
- item
phpcbf_executable_path
should be (full line statement):"phpcbf_executable_path": "/Users/your_user_name/.composer/vendor/bin/phpcbf",
- item
phpcbf_additional_args
should be (full line statement):
"phpcbf_additional_args": {
"--standard": "WordPress",
"-n": ""
},
That should be it, you have now a context menu item "PHP Code Sniffer" in Sublime text where you can sniff, change default, and Beautify.
Note, Sublime package additionally adds "php cs fixer" as well, which will not work with WPCS. Ignore and do not use it.
Note 2, The SublimeLinter-phpcs
(second method described on above doc) did not work for me at all. While it is available in the package control for install, it will fail with ImportError: No module named 'SublimeLinter'
on install.
Why, not sure, and did not dig, and removed it again.
I am adding here a note that MacOS users of Monterey might run into an error env: php: No such file or directory
This would be the error you see in the terminal after running phpcs --standard=WordPress your-file.php
However in an IDE like Sublime Text you won't see any error, things just won't work anymore, at all but the commands will be there.
If that is the case for you too, and you use Monterey - then be aware that they removed PHP. You will need to install it.
A possible approach is (terminal) brew install php
, which will take a while and install PHP, and things will be back working.
Updating this 2 years later, if you still get the Sublime text doing "nothing" when saving, open the console
of Sublime text and check the errors.
You might still see env: php: No such file or directory
in which case, if the command phpcs --standard=WordPress your-file.php
works in the terminal but not in the sublime, you might need to run brew link [email protected]
7.4 because WPCS lives in 2020, and somehow forgot that we only have one maintained PHP version nowadays. Development is in process about that so, if WPCS 3 ever gets released, you would not need php 7.4 anymore.
I do hope this helps anyone. It certainly helped me now the 5th time, and this doc is still not working...
Well. The original page seems to be deleted meanwhile. Not going to hunt his anymore therefore.
@smileBeda It has not been deleted, though might have been renamed: https://github.com/WordPress/WordPress-Coding-Standards/wiki/Setting-up-WordPressCS-to-work-in-Sublime-Text