Git Pre-commit Hook for WordPress projects

Wouldn’t it be cool if you, in your PHP project could define a package that makes sure that everybody who even tries to commit code is forced to follow the project’s defined coding standards? Yeah, me too. So I created this Composer plugin you can use in your projects.

This package will install PHPCS with the WordPress Coding Standard definitions together with a git pre-commit hook script that will run PHP linting and the PHPCS whenever you do a commit to your project.

If your project has a phpcs.ruleset.xml file available, the pre-commit hook will use it as a ruleset definition. Otherwise the “WordPress” ruleset will be used.


composer require bjornjohansen/wp-pre-commit-hook --dev

Or manually add "bjornjohansen/wp-pre-commit-hook": "*" to the require-dev section in your composer.json

Then do a composer update

Now a Git pre-commit hook should have been installed, which will stop people from commit invalid PHP code or code that doesn’t follow your project’s defined ruleset.

In case you want to inspect what the script actually does, or contribute to improve it, it is available on GitHub.

What about MS Windows?

Oh, crap, are you using Windows? Well, I don’t. I don’t know anything about PowerShell, so I can’t replicate the functionality there. I’ve added a check, so it shouldn’t break anything if you’re on Windows, though. It just doesn’t do anything.

As this is just a helpful addon, and not a strict dependency in your project, you should be safe to use it, even of some of your developers use MS Windows.