Git Hooks with Husky and lint-staged for WordPress Projects

Git hooks with Husky and lint-staged in a WordPress project

A coding standards check in CI catches problems after the push, and the developer has to fix and push again. A pre-commit hook catches them before the commit exists. Husky installs git hooks from the repository for everybody who runs npm install, and lint-staged runs the linters only on the files that are staged, so the hook takes seconds instead of linting the whole theme.

Problem: A theme repository has PHPCS, ESLint and Stylelint configured, but developers forget to run them. CI fails on half of the pull requests for formatting, and the fix commits clutter the history.

Solution: Add Husky and lint-staged as dev dependencies, create a pre-commit hook that runs lint-staged, configure it to run PHPCBF and PHPCS on staged PHP files, ESLint with --fix on JavaScript and Stylelint on CSS, and keep the same checks in CI as the authority.

Install

npm install --save-dev husky lint-staged
npx husky init

husky init creates the .husky/ folder with a sample pre-commit file and adds a prepare script to package.json, which runs husky on every npm install and sets core.hooksPath to .husky. New team members get the hooks without any extra step.

# .husky/pre-commit
npx lint-staged

lint-staged configuration

{
    "lint-staged": {
        "*.php": [
            "vendor/bin/phpcbf --standard=phpcs.xml.dist",
            "vendor/bin/phpcs --standard=phpcs.xml.dist"
        ],
        "*.{js,jsx}": "wp-scripts lint-js --fix",
        "*.{css,scss}": "wp-scripts lint-style --fix",
        "*.json": "prettier --write"
    }
}

This goes into package.json. lint-staged passes the staged files that match each pattern to the commands, in order. Fixers (phpcbf, --fix) change the files, and lint-staged adds those changes to the commit automatically. If a command exits with an error, the commit is aborted and the output shows what is wrong.

phpcbf exits with code 1 when it fixed something, which lint-staged would treat as a failure. Wrap it so that only “could not fix” counts:

#!/usr/bin/env bash
# bin/phpcbf-staged.sh
vendor/bin/phpcbf --standard=phpcs.xml.dist "$@"
code=$?
[ "$code" -le 1 ] && exit 0 || exit "$code"

and use "bin/phpcbf-staged.sh" as the first command for *.php.

Why only staged files

A pre-commit hook that runs PHPCS over the whole plugin takes a minute and reports problems in files the developer did not touch, so people start using git commit --no-verify. On staged files it takes a few seconds and only reports what this commit changes. lint-staged also stashes unstaged changes in the same files while it runs, so partial staging with git add -p keeps working.

A commit-msg hook too

# .husky/commit-msg
npx --no -- commitlint --edit "$1"

With @commitlint/config-conventional, commit messages must follow Conventional Commits, which makes changelogs generated from history possible: Conventional Commits and git-cliff.

CI stays the authority

Hooks can be skipped (--no-verify, a GUI client, a missing npm install). Run the same commands over the whole code base in CI: GitHub Actions CI/CD for WordPress plugins. The hook is a fast first check, CI is the rule.

NOTE: Husky needs Node.js on every developer machine, which a PHP-only plugin may not have. Without Node.js, a plain .githooks/pre-commit script with git diff --cached --name-only --diff-filter=ACM -- '*.php' | xargs -r vendor/bin/phpcs and git config core.hooksPath .githooks does the same for PHP.