Composer is a PHP-dependency supervisor, a software to import and handle our PHP venture dependencies (each our personal and from Third-party suppliers). It really works with PHP packages (the place a package deal is basically a listing containing PHP code), that are retrieved from package deal repositories, both public or personal.

Composer permits to bootstrap a brand new PHP-based utility very simply: we outline the record of dependencies our venture is dependent upon and, by executing a single command, Composer can have them put in and up to date to their newest variations (or to whichever model we require).

On this article we are going to learn to automate the creation of WordPress websites via Composer and WP-CLI (which permits to work together with WordPress by executing instructions on the command-line), as to save lots of time when testing, creating and deploying our web sites.

Including Composer to a venture

To begin utilizing Composer, all we’d like is a composer.json file, a JSON file positioned on the root of our venture, defining the package deal’s identify and its dependencies:

{
“name”: “packageVendor/projectName”,
“require”: {
“packageName”: “versionContraint”,
“packageName”: “versionContraint”,
“…”: “…”
}
}

The package deal identify consists of a package deal vendor and venture identify, in format “packageVendor/packageName”. This format permits to add our package deal to the repository and never collide with packages from different distributors. On this case, my vendor identify is “leoloso”, and the identify I selected for my venture is “wp-install”:

{
“name”: “leoloso/wp-install”
}

Our venture dependencies are outlined underneath the require entry. Every dependency declares the package deal identify as its key and, as its worth, the package deal’s model constraint, which signifies which model of the package deal we wish to set up. We will outline any package deal hosted in Composer’s default package deal repository, Packagist.

For example, we are able to set up the most recent model of logging library Monolog like this:

{
“require”: {
“monolog/monolog”: “2.0.*”
}
}

As soon as we’ve got composer.json configured, we are able to set up all of the venture dependencies by working composer set up within the terminal (on the root folder of our venture), and replace them to their newest variations by working composer replace.

Putting in WordPress via Composer

With a view to set up WordPress via Composer, we should deal with WordPress as one more dependency. On this sense, despite the fact that WordPress is the underlying CMS for our utility, putting in it’s no totally different than putting in a easy PHP library. Since there is no such thing as a “official” WordPress composer package deal, we are able to use the one created by John P. Bloch, like this:

{
“require”: {
“johnpbloch/wordpress”: “>=5.3”
}
}

WordPress themes and plugins, as accessible underneath the WordPress plugin and theme directories, are hosted via a devoted package deal repository, WPackagist. We might want to outline it in our composer.json file, like this:

{
“repositories”: [
{
“type”: “composer”,
“url”: “https://wpackagist.org”
}
]
}

When declaring themes and plugins as dependencies, their vendor names are “wpackagist-theme” and “wpackagist-plugin” respectively. For example, putting in the Gutenberg plugin is finished like this:

{
“require”: {
“wpackagist-plugin/gutenberg”: “>=7.2”
}
}

Making a Composer venture

We noticed how one can outline all our dependencies in our venture and have Composer set up them by working a command within the terminal. Subsequent step is to transform our venture right into a “template” venture, from which we are able to simply bootstrap any new WordPress web site.

For this, we first outline our venture being of sort “project” (versus the default sort “library”) within the composer.json file:

{
“type”: “project”
}

It is strongly recommended to additionally outline the venture’s homepage, description, license and creator info. On this case, the venture’s supply code is in GitHub, so I simply outline its repo URL:

{
“homepage”: “https://github.com/leoloso/wp-install”,
“description”: “Bootstrap a WordPress site through Composer and WP-CLI”,
“license”: “MIT”,
“authors”: [
{
“name”: “Leonardo Losoviz”,
“email”: “…”,
“homepage”: “…”
}
]
}

If we haven’t executed so but, we should add our venture as a package deal in some repository. The best choice is to submit it to Packagist, for which we should simply add the repo URL within the Packagist submission web page:

Packagist submission page

As soon as our package deal is on the market, we are able to bootstrap a brand new WordPress web site by executing Composer’s “create-project” command, which creates a brand new venture from the indicated package deal.

For example, to spin up a brand new WordPress web site utilizing venture leoloso/wp-install, and create it underneath folder new_wp_site, we execute this command within the terminal:

composer create-project leoloso/wp-install new_wp_site

This instructions means: git clone the venture from https://github.com/leoloso/wp-install into folder “new_wp_site”, step into the folder, and execute composer set up to obtain and set up all of its dependencies.

Putting in WordPress

To this point so good: after executing the create-project command we can have WordPress, its theme and plugins downloaded into the venture folder. Subsequent step is to run a script to configure and set up WordPress.

If coping with a single web site, we could also be tempted to add file wp-config.php (the place the WordPress configuration entries, such because the database identify, consumer and password, are outlined) to the repo. Though that is flawed (since importing credentials to the repo is a safety threat, and it doesn’t enable to simply scale up deploying the software program, as really helpful by the 12-Issue App), it will work. Nonetheless, for utilizing a venture to spin up a number of web sites it won’t work, as a result of every web site will want its personal configuration.

The answer is to have file wp-config.php empty, and to fill it up via atmosphere variables, which, as well as, permits to automate the creation of various web sites from the identical supply repo, or the identical web site for various environments (DEV, STAGING, PROD).

We can have WP-CLI obtain this technique: retrieve the required configuration gadgets from atmosphere variables and outline them on the configuration file. After establishing the configuration file, WP-CLI can proceed to put in WordPress, which entails creating all of the tables within the database, and configuring the applying with the web site identify, URL, admin individual’s e mail, username and password. This course of will likely be triggered via Composer occasions, invoked proper after the creation of a brand new venture.

Let’s proceed to do it. All of the atmosphere variables that will likely be required are the next:

# For wp-config.php
$DB_NAME: The identify of the database for WordPress
$DB_USER: MySQL database username
$DB_PASSWORD: MySQL database password
$DB_HOST: MySQL hostname

# For putting in WordPress
$SITE_URL_WITHOUT_HTTP: Similar to www.mywebsite.com
$SITE_URL_WITH_HTTP: Similar to https://www.mywebsite.com
$SITE_NAME: Web site identify
$ADMIN_USER: Admin individual’s username
$ADMIN_PASSWORD: Admin individual’s password
$ADMIN_EMAIL: Admin individual’s e mail

Therefore, earlier than creating the brand new venture, we might want to set-up its atmosphere variables. For this, we are able to use the terminal to export their values, like this:

export DB_NAME=my_wp_site_db
export DB_USER=admin
export DB_PASSWORD=saranbadangananga
export DB_HOST=127.0.0.1
export SITE_URL_WITHOUT_HTTP=www.mywebsite.com
export SITE_URL_WITH_HTTP=https://www.mywebsite.com
export SITE_NAME=”My tremendous superior tremendous cool new WP web site”
export ADMIN_USER=admin
export ADMIN_PASSWORD=ermenegildo
export [email protected]

Composer permits to execute scripts throughout the lifecycle of the create-project command, for particular occasions. What scripts are executed for what occasions is configured in file composer.json underneath entry “scripts”.

We should first validate that every one required atmosphere variables have been outlined, which shall be executed via a script file set up/validate-env-variables.sh, executed upon occasion “post-root-package-install” (which happens after the basis package deal has been put in):

{
“scripts”: {
“post-root-package-install”: [
“./install/validate-env-variables.sh”
]
}
}

Script file validate-env-variables.sh will examine if each one of many required atmosphere variables has been set and, if not, present an error message and terminate the method. Its content material is the next:

#!/bin/bash

# Flag to know if there are errors
ERROR_ENV_VARS=””

# Required for wp-config.php
if [ -z “$DB_NAME” ]
then
ERROR_ENV_VARS=”$ERROR_ENV_VARSnDB_NAME”
fi
if [ -z “$DB_USER” ]
then
ERROR_ENV_VARS=”$ERROR_ENV_VARSnDB_USER”
fi
if [ -z “$DB_PASSWORD” ]
then
ERROR_ENV_VARS=”$ERROR_ENV_VARSnDB_PASSWORD”
fi
if [ -z “$DB_HOST” ]
then
ERROR_ENV_VARS=”$ERROR_ENV_VARSnDB_HOST”
fi

# Required for putting in WordPress via WP-CLI
if [ -z “$SITE_URL_WITHOUT_HTTP” ]
then
ERROR_ENV_VARS=”$ERROR_ENV_VARSnSITE_URL_WITHOUT_HTTP”
fi
if [ -z “$SITE_URL_WITH_HTTP” ]
then
ERROR_ENV_VARS=”$ERROR_ENV_VARSnSITE_URL_WITH_HTTP”
fi
if [ -z “$SITE_NAME” ]
then
ERROR_ENV_VARS=”$ERROR_ENV_VARSnSITE_NAME”
fi
if [ -z “$ADMIN_USER” ]
then
ERROR_ENV_VARS=”$ERROR_ENV_VARSnADMIN_USER”
fi
if [ -z “$ADMIN_PASSWORD” ]
then
ERROR_ENV_VARS=”$ERROR_ENV_VARSnADMIN_PASSWORD”
fi
if [ -z “$ADMIN_EMAIL” ]
then
ERROR_ENV_VARS=”$ERROR_ENV_VARSnADMIN_EMAIL”
fi

# If there are errors, return an error state
if [ -n “$ERROR_ENV_VARS” ]
then
RED=’