Skip to content
Corey M Collins edited this page May 24, 2022 · 2 revisions

Composer Config

In general, this file shouldn't look all that different from our package.json file or other config files found in the theme.

In this config, we're specifying PHP 7.4 and Composer 2.0 while tapping into a handful of plugins to allow for our scripts to run:

wd_s uses Composer to run a few different processes:

Our Config File

This is what our Composer file looks like:

{
	"name": "webdevstudios/wd_s",
	"description": "A starter theme from WebDevStudios.",
	"type": "wordpress-theme",
	"license": "GPL-2.0-or-later",
	"authors": [
		{
			"name": "WebDevStudios",
			"email": "contact@webdevstudios.com"
		}
	],
	"config": {
		"platform": {
			"php": "7.4"
		},
		"allow-plugins": {
			"composer/installers": true,
			"dealerdirect/phpcodesniffer-composer-installer": true
		}
	},
	"require": {
		"composer/installers": "^2.0.0"
	},
	"require-dev": {
		"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
		"phpcompatibility/phpcompatibility-wp": "^2.1.1",
		"wp-cli/wp-cli-bundle": "^2.5.0",
		"wp-coding-standards/wpcs": "^2.3.0"
	},
	"scripts": {
		"format": "phpcbf --report=summary,source",
		"lint": "phpcs --report=summary,source",
		"pot": "wp i18n make-pot . build/languages/_s.pot --exclude=node_modules,vendor,build --allow-root"
	}
}

PHP Linting

In our package.json file, lint:php taps into Composer by running composer run-script lint. In doing so, we're looking for any issues found in our PHP issues related to coding standards. As this is a WordPress theme, we default to WordPress coding standards to keep in line with WordPress' overall goals for code quality.

PHP Code Sniffer will also find any warnings or breaking errors in your PHP, so if you forget to add that semicolon or have some code that may be prone to security or performance issues, PHPCS will let you know.

As part of our Lefthook configuration, all code is linted in a pre-commit hook so the chance of committing code with formatting issues should be low.

PHP Formatting

In our package.json file, format:php taps into Composer by running composer run-script format. In doing so, we're fixing any PHP issues that are able to be fixed automatically. You may still have linting errors or breaking errors in your PHP if the issues are too complex for PHPCBF to fix by itself, but the majority of coding standards issues should be taken care of here.

As part of our Lefthook configuration, all code is formatted in a pre-commit hook so the chance of committing code with formatting issues should be low.

Language File Generation

Of course, you've written your theme to be translatable into other languages. In our package.json file, build:pot taps into Composer by running composer run-script pot. In doing so, we're building the .pot file for our theme. This is the language file which contains the strings in your base language which can then be translated into other languages.

Clone this wiki locally