How to install PHP Linting and Coding Standards For WordPress

Post author: Adam VanBuskirk
Adam VanBuskirk
5/17/23 in
Tech
PHPWordPress

This article covers step-by-step instructions for installing a linter for WordPress PHP coding standards and configuring it to prevent pushing to the remote GitHub repository if any coding violations are found. These instructions assume that you are using a Mac and that you already have VSCode and Git installed on your Macbook.

Install Composer on Your Mac

You will need composer installed on your Mac. Check if it is installed by opening up your terminal and running: composer --version. If you see Composer version number, then it is installed.

If not, download it by pasting – https://getcomposer.org/composer.phar in your browser. This will download the file. Next, run the below command to test it.

php ~/Downloads/composer.phar --version

Let’s move it to bin directory to make composer widely available to run on the Mac. Then we’ll make it executable and run another version test.

cp ~/Downloads/composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

Install The WordPress Coding Standards and PHP_CodeSniffer

From the Mac terminal, we’re going to create a folder named DevTools, change directory into it, and clone the GitHub project into it which contains the WordPress Coding Standards. We then change directory into the WordPress-Coding-Standards folder which contains the coding standards and run composer install.

mkdir DevTools
cd DevTools
git clone https://github.com/WordPress/WordPress-Coding-Standards.git
cd WordPress-Coding-Standards
composer install

Install Visual Studio Code Extension & Apply Coding Standards

  1. Open VSCode and open the extensions panel by clicking on the extensions icon in the left-hand sidebar or by pressing Ctrl+Shift+X (Windows/Linux) or Cmd+Shift+X (Mac).
  2. Locate and install the PHP Sniffer & Beautifier by Samual Hilson.
  3. Once the extension is installed, open the VSCode settings by clicking on the gear icon in the lower-left corner of the VSCode window and selecting “Settings” or by pressing Ctrl+, (Windows/Linux) or Cmd+, (Mac). Click the edit in settings.json link and paste the below into it and save.
    "phpsab.executablePathCBF": "/Users/adamvanbuskirk/DevTools/WordPress-Coding-Standards/vendor/squizlabs/php_codesniffer/bin/phpcbf",
    "phpsab.executablePathCS": "/Users/adamvanbuskirk/DevTools/WordPress-Coding-Standards/vendor/squizlabs/php_codesniffer/bin/phpcs",
    "phpsab.autoRulesetSearch": false,
    "phpsab.standard": "WordPress",
    "phpsab.snifferArguments": [
        "--ignore=*/wp-admin/*,*/wp-includes/*"
    ]

Now, if I visit any PHP script, right-click and select Format Document, it automatically formats my doc to the WordPress Coding Standards.

Globally Install The Code Sniffer

composer global require "squizlabs/php_codesniffer=*"

That’s it. We now have WordPress Coding Standards installed and as you can see below, it is highlighting my violations. You can hover over the red squiggly lines to see the exact violation. After fixing, simply save the file to see the violations go away. Next, we’ll be setting up a GitHub hook to prevent us from pushing our code to the repo if violations exist.

Using GitHub Hooks to Prevent Code Violations From Being Committed

Next, I navigate into my local Git repo, which for me is my wp-content folder. Then, click SHIFT + COMMAND + . to show hidden files, which includes the .git folder. Navigate to the .git > hooks folder.

Duplicate the pre-commit.sample file and rename it pre-commit, dropping the sample.

Open it with VS Code, blank out its contents, and paste in the below. Make sure to change the paths to the phpcs and phpcbf files to your path where you have the WordPress coding standards installed.

#!/bin/bash

PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`

# Determine if a file list is passed
if [ "$#" -eq 1 ]
then
    oIFS=$IFS
    IFS='
    '
    SFILES="$1"
    IFS=$oIFS
fi
SFILES=${SFILES:-$STAGED_FILES_CMD}

echo "Checking PHP Lint..."
for FILE in $SFILES
do
    php -l -d display_errors=0 "$PROJECT/$FILE"
    if [ $? != 0 ]
    then
        echo "Fix the error before commit."
        exit 1
    fi
    FILES="$FILES \"$PROJECT/$FILE\""
done

if [ -f "$PROJECT/phpcs.ruleset.xml" ]
then
    RULESET="$PROJECT/phpcs.ruleset.xml"
elif [ -f "$PROJECT/phpcs.xml.dist" ]
then
    RULESET="$PROJECT/phpcs.xml.dist"
else
    RULESET="WordPress"
fi

echo "Checking Code Standard Compliance, using $RULESET as ruleset standard..."
for FILE in $SFILES
do
    /Users/adamvanbuskirk/DevTools/WordPress-Coding-Standards/vendor/squizlabs/php_codesniffer/bin/phpcs --standard="$RULESET" --colors --encoding=utf-8 -n -p "$PROJECT/$FILE"
    if [ $? != 0 ]
    then
        echo "Fix the error before commit."
        echo "Run"
        echo "  /Users/adamvanbuskirk/DevTools/WordPress-Coding-Standards/vendor/squizlabs/php_codesniffer/bin/phpcbf --standard=\"$RULESET\" $FILES"
        echo "for automatic fix or fix it manually."
        exit 1
    fi
done

exit $?

Now, when I attempt to commit changes that violate the WordPress standards, it throws errors forcing me to fix them.

That’s it. We’re all set. On a parting note, I can go through and fix the issues manually or I could run the below to automatically fix the issues in my two files.

/Users/adamvanbuskirk/DevTools/WordPress-Coding-Standards/vendor/squizlabs/php_codesniffer/bin/phpcbf --standard="WordPress"

"/Users/adamvanbuskirk/Local Sites/wordbot/app/public/wp-content/themes/wordbot/single.php"

Sign up today for our weekly newsletter about AI, SEO, and Entrepreneurship

Leave a Reply

Your email address will not be published. Required fields are marked *


Read Next




© 2024 Menyu LLC