• Stars
    star
    191
  • Rank 201,579 (Top 4 %)
  • Language
  • License
    MIT License
  • Created almost 8 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

How to install PHP CodeSniffer and the WordPress Coding Standard Rules in Visual Studio Code.

PHP CodeSniffer and WordPress Coding Standards with VS Code

Last Updated 2020-07-31 by @tommcfarlin

This guide is meant to provide all of the steps necessary to easily get up and running with PHP CodeSniffer, the WordPress Coding Standard ruleset, and Visual Studio Code.

Visual Studio Code

All of the resources used in this guide are linked at the bottom. This guide is also licensed MIT. If you'd like to contribute, then please feel free to open issues or issue pull requests. I'll be happy to merge them and also add your username to CONTRIBUTING.

If you're looking for corresponding blog posts, please see:

  1. Setting Up PHP CodeSniffer in Visual Studio Code
  2. Setting Up PHP CodeSniffer Per Project

As always, don't forget to checkout the CHANGELOG to track everything that's changed since the initial release of this guide.


1. Verifying PHP

The following steps assume you have PHP installed and globally accessible on your system. You can test this by entering the following command in the terminal:

$ php -v

And you should see something like this:

PHP 7.4.7 (cli) (built: Jun 12 2020 00:04:10) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans
    with Zend OPcache v7.4.7, Copyright (c), by Zend Technologies

If you're looking for how to use a different version of PHP installed elsewhere on your system, this is not the guide for that. If, however, you're curious as to where the version of PHP you're using is stored, you can enter:

$ which php

And you should see something similar to this:

/usr/local/bin/php

That should give you enough information for the rest of this guide.

2. Installing Composer

Installing Composer globally means that you'll be able to access it from anywhere on your system (that is, in any directory regardless of where you are). To do this, you can read the manual or follow the quick steps below (which summarize the manual, anyway):

  1. Grab the latest snapshot of Composer. Save it somewhere you'll remember.
  2. Move the file you just downloaded to the /usr/local/bin/ directory on your machine.

To do this, open your terminal navigate to the directory where you downloaded composer.phar. Move the file to the diretory mentioned above by issuing the following command:

$ mv composer.phar /usr/local/bin/composer

And now you can access Composer from anywhere in your system. To do try it out, enter the following command in your terminal:

$ composer about

You should see something like this:

Composer - Package Management for PHP
Composer is a dependency manager tracking local dependencies of your projects and libraries.
See https://getcomposer.org/ for more information.

With Composer globally installed, you can now install the WordPress Coding Standards rules.

3. Installing PHP CodeSniffer

For the purposes of this document, we're installing PHP CodeSniffer on a project-by-project basis. To do this, we're going to be using Composer.

From the integrated terminal within Visual Studio Code, enter the following command:

$ composer require "squizlabs/php_codesniffer=3.*"

This will create composer.json, tell it where to locate the PHP CodeSniffer, and install it in a vendor directory. Once this is done, we need the WordPress Coding Standard ruleset.

4. Installing the WordPress Coding Standards Rules

I recommend placing the rules in a directory you can refer to often. Personally, I use a projects directory to manage all of my work.

From within your directory of choice, say /projects, enter the following command in the terminal:

$ composer create-project wp-coding-standards/wpcs:dev-master --no-dev

This will create a wpcs directory in your projects directory and it makes it easy to tell each project where the WordPress Coding Standards are stored because, remember, we'll be using Composer on a project-by-project basis.

5. Tell PHPCS About WPCS

From within Visual Studio's integrated terminal, make sure that you're in your project's directory and then issue the following command:

$ ./vendor/bin/phpcs --config-set installed_paths /path/to/dropbox/projects/wpcs

And this will tell your project's copy of PHPCS where the WordPress Coding Standards are.

6. Update User Settings

Finally, we need to let Visual Studio what we're going to be using to sniff out the code in our project and what rules to use. This is really easy to do. In Visual Studio, hit the ⌘, (or whatever your operating system uses) command to open settings.json.

Make sure the file looks like the following (though you may need to tweak based on your existing settings)

// Place your settings in this file to overwrite the default settings
{
    // PHPCS
    "phpcs.enable": true,
    "phpcs.standard": "WordPress",
    "phpcs.executablePath": "./vendor/bin/phpcs",
    "phpcs.showWarnings": true,
    "phpcs.composerJsonPath": "composer.json",
}

And this will enable PHPCS and will also tell it to use the standard WordPress ruleset. If this doesn't start working on the code your have automatically, then restart Visual Studio Code.


Resources

More Repositories

1

WordPress-Widget-Boilerplate

[WordPress] The WordPress Widget Boilerplate is an organized, maintainable boilerplate for building widgets using WordPress best practices.
PHP
1,016
star
2

WordPress-Settings-Sandbox

[WordPress-Envato] A simple theme used to showcase the WordPress Settings API. The corresponding series of articles will run on Envato's TutsPlus Network.
PHP
259
star
3

page-template-example

[WordPress] An example WordPress plugin used to show how to include templates with your plugins and programmatically add them to the active theme.
PHP
229
star
4

konami-code

[jQuery] Using the Konami code, easily configure and Easter Egg for your page or any element on the page.
JavaScript
139
star
5

simple-autoloader-for-wordpress

An autoloader that aims to be as simple as dropping it into your WordPress project. All you need is a well-organized project.
PHP
89
star
6

theme-customizer-example

[WordPress-Envato] This theme is used to demonstrate how to integrate the Theme Customizer into a WordPress theme. It's an example project used in a WPTuts+ series of articles.
PHP
89
star
7

single-post-meta-manager

[WordPress] A plugin for displaying a given post's meta data within the single post view of editing a post.
PHP
56
star
8

Simple-Overlay

[Retired-jQuery] A Plugin For Lightweight, Flexible, and Customizable Overlays. This plugin is no longer maintained.
JavaScript
54
star
9

wp-plugin-scaffold

A simple scaffold used for what's needed to spin up a Composer-based WordPress plugin.
PHP
47
star
10

title-capitalization-for-wordpress

[WordPress] Properly capitalizes post titles and heading elements in the post content when saving and updating posts.
PHP
46
star
11

wp-gist

Adds support for GitHub Gist embeds to WordPress.
JavaScript
43
star
12

Basic-Theme

[Retired-WordPress-Envato] A simple theme used to demonstrate how to introduce unit testing into WordPress theme development.
PHP
38
star
13

toggle-admin-notices

Hide all admin notices, warnings, and errors in the WordPress admin area using an option in the admin bar.
PHP
37
star
14

markdown-code-for-wordpress

[WordPress] A simple WordPress plugin for replacing backticks with `code` tags in the content of posts, pages, and other post types.
PHP
33
star
15

page-template-info

[WordPress] This plugin displays the name of the template associated with each page on the 'All Posts' page.
PHP
29
star
16

page-template-dashboard

An easy way to see which templates your pages are using without having to view the page editor.
PHP
27
star
17

Hello-Reader

[Retired-WordPress-Envato] A simple WordPress plugin used to demonstrate unit testing in the context of WordPress projects.
PHP
23
star
18

namespaces-and-autoloading-in-wordpress

[WordPress] A simple example of namespaces an autoloading used to demonstrate the concepts in a presentation at WordCamp Atlanta 2017.
PHP
22
star
19

Collapsible-Menus

[Retired-jQuery] Collapsible Menus is a plugin that makes it easy to create collapsible menus using nested, unordered lists.
JavaScript
20
star
20

WordPress-Custom-Menu-Separator

[WordPress] A simple plugin for demonstrating how to add a custom menu separator in the WordPress administration menu.
PHP
19
star
21

wp-remote-post-example

[Retired-WordPress-Envato] An example plugin demonstrating how to use wp_remote_post.
PHP
17
star
22

remove-empty-shortcodes

Removes the Restrict Content Pro shortcode from your content.
PHP
15
star
23

wp-simple-ajax

[Envato] A simple demo for how to use Ajax in WordPress. The repository will ultimately contain two tags - one for a procedural approach to the plugin, one for an object-oriented approach to the plugin.
PHP
15
star
24

ajax-notification

[Retired-WordPress-Envato] An example plugin used to demonstrate the WordPress Ajax API for a companion article on Envato's WPTuts+ site.
PHP
14
star
25

WordPress-Upload-Meta-Box

[WordPress] An example plugin for how to include a metabox for attaching files to your WordPress posts outside of the media uploader.
PHP
14
star
26

generic-custom-post-type

Creates a generic post type to be used for testing other plugins that rely on them.
PHP
14
star
27

My-Social-Networks

[Retired-WordPress-Envato] An simple plugin demonstrating how to write maintainable WordPress widgets.
PHP
14
star
28

simple-factory-example

[Retired-WordPress-Envato] An example of how to use the Simple Factory design pattern.
PHP
11
star
29

Redactor-Image-Upload-For-WordPress

[WordPress] A script that aims to make it easy to integrate image uploads with the Redactor.js WYSIWYG editor into any WordPress based project.
PHP
10
star
30

ive-read-this

[Retired-WordPress-Envato] A simple plugin used to demonstrate how to use Ajax on the frontend of WordPress
PHP
10
star
31

Post-Author-Credit

[Retired-WordPress] A simple plugin used to demonstrate how to use Ajax in WordPress development. Includes fully documented and localized code following WordPress coding conventions. This plugin is no longer maintained.
PHP
10
star
32

WordPress-jQuery-Date-Picker

[Retired-WordPress-Envato] An example plugin for how to include the jQuery date picker into the WordPress post dashboard.
PHP
9
star
33

remove-public-pingbacks

Removes the count and listing of pingbacks from the public-facing side of a WordPress site.
PHP
9
star
34

gURLDemo

[Retired] A simple demonstration of how to use the Google URL shortener API in your own web-based projects. This plugin is no longer maintained.
PHP
9
star
35

tags-without-links

[WordPress] This plugin makes it easier for developers to display tags without any links associated with them.
PHP
8
star
36

tm-callback-info

Renders contextual information about every function registered with all WordPress hooks.
PHP
8
star
37

add-custom-view

A WordPress plugin that demonstrates how to add custom views to the Edit Page.
PHP
7
star
38

jquery-get-input-type

[jQuery] A simple plugin used for getting any given input element's type.
JavaScript
7
star
39

Top-Commenters-Cached

[Retired-WordPress-Envato] A plugin used to demonstrate the WordPress Transients API for an Envato blog series. This plugin is no longer maintained.
PHP
7
star
40

RSS-Note

[Retired-WordPress-Envato] A plugin used to demonstrate how to write maintainable plugins. This plugin is no longer maintained.
PHP
6
star
41

three-recent-posts

An example project that goes from rapid-application-development to a full-fledged plugin for WordPress.
PHP
6
star
42

wp-env-reader-drop-in

A drop-in utility for WordPress projects that reads information from a `.env` file to make testing and simulating easier.
4
star
43

custom-metadata-filter

Adds a custom filter for viewing posts with headline metadata.
4
star
44

category-sticky-post

Mark a post to be displayed – or stuck – to the top of the archive page for the specified category.
4
star
45

cmb2-featured-mobile-image

Select an image to display as your featured image in the mobile version of your site.
PHP
4
star
46

acme-company

[WordPress] An example for how to use interfaces and inheritance with the WordPress Settings API.
PHP
3
star
47

wp-dracula-highlight-js

A WordPress plugin for adding the Dracula color scheme and Highlight.js to the Block Editor.
PHP
3
star
48

wp-hello-world

An example plugin for a membership course walking readers through how to work with GitHub.
PHP
3
star
49

disable-all-comments

Disables all WordPress comments while also allowing you to turn them back on at a per-post level.
PHP
2
star
50

custom-comment-emails

[Retired-Envato] A plugin used to demonstrate how to create custom comment email notifications. The corresponding series of articles will run on Envato's TutsPlus Network.
PHP
2
star
51

backcast

An application used to backup podcast subscriptions via XML export files.
1
star
52

subtitles-migration

Migrates old theme subtitles to compatibility with the Subtitles plugin.
PHP
1
star
53

ray-for-wordpress

An example project for how to use Spatie Ray in WordPress Development.
PHP
1
star
54

wp-template-view

Easily see what custom templates your pages and custom post types are using from the WordPress administration area.
1
star