Composer is a dependency management tool for PHP that allows you to declare, install, and manage external libraries and packages in your projects. It provides a convenient way to handle dependencies, making it easier to integrate third-party code into your WordPress development workflow.
Why use composer.json with WordPress?
Using composer.json with WordPress brings several benefits to your development workflow. Here are a few reasons why you should consider incorporating Composer into your WordPress projects:
- Dependency management: Composer allows you to define and manage external dependencies for your WordPress project. This ensures that the required libraries and packages are present and up to date, making it easier to maintain and update your application.
- Efficient updates: With Composer, you can easily update your WordPress plugins, themes, and other dependencies with a single command. It saves time and effort by automating the process and ensuring compatibility between different components.
- Version control integration: Composer generates a
composer.lock
file that locks the versions of your dependencies. This file can be committed to your version control system, ensuring that all team members work with the same set of dependencies. - Autoloading classes: Composer provides an autoloading mechanism that simplifies the inclusion of external libraries and namespaces in your WordPress project. This eliminates the need for manual
require
statements and improves code organization.
In my WordPress development workflow, I use Composer to install plugins instead to download directly inside a Docker environment.
This is the folder structure:
my-project/
├── bin/
│ ├── wordpress/
│ │ ├── Dockerfile
│ │ └── composer.json
├── docker-compose.yml
├── database/
├── uploads/
├── .env
Inside the composer.json we insert all plugins that we need to install like this:
{
"name": "my-project",
"description": "",
"authors": [
{
"name": "Lorenzo Sfienti",
"email": "[email protected]"
}
],
"repositories":[
{
"type":"composer",
"url":"https://wpackagist.org"
}
],
"require": {
"wpackagist-plugin/classic-editor":"1.6.3",
"wpackagist-plugin/wordpress-seo":"20.8"
},
"autoload": {
"psr-0": {
"Acme": "src/"
}
},
"config": {
"allow-plugins": {
"composer/installers": true
}
},
"extra" : {
"installer-paths": {
"wp-content/plugins/{$name}/": ["type:wordpress-plugin"],
"wp-content/themes/{$name}/": ["type:wordpress-theme"]
}
}
}
In this example, I add two plugins:
- classic-editor
- wordpress-seo
To find the name and the correct version of each plugin you need to use WordPress Packagist. This website collects all plugins that you find inside the WordPress Official Plugin Directory.
When you complete the list of necessary plugins you create a Dockerfile like this:
FROM composer as build
WORKDIR /var/www/html/wp-content
RUN curl -sS https://getcomposer.org/installer | \
php -- --install-dir=/usr/bin/ --filename=composer
COPY composer.json /var/www/html/wp-content
RUN composer update
RUN composer dump-autoload --optimize
FROM wordpress:latest
COPY --from=build /var/www/html/wp-content/wp-content/plugins /var/www/html/wp-content/plugins
RUN apt-get update && apt-get install -y less
# Install WPCLI
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
RUN chmod +x wp-cli.phar
RUN mv wp-cli.phar /usr/local/bin/wp
This Dockerfile first of all uses the Docker image of the composer file to install all dependencies. In the second step use the Official Docker image for WordPress and copy the build package to the plugins folder of the WordPress installation.
In the final step create a docker-compose file like this:
services:
db:
image: mariadb:10.6.4-focal
command: '--default-authentication-plugin=mysql_native_password'
restart: always
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
expose:
- 3306
volumes:
- ./database:/var/lib/mysql
wordpress:
build:
context: ./bin/wordpress
volumes:
- ./uploads:/var/www/html/wp-content/uploads
ports:
- ${HTTP_PORT}:80
restart: always
environment:
- WORDPRESS_DB_HOST=${MYSQL_HOST}
- WORDPRESS_DB_USER=${MYSQL_USER}
- WORDPRESS_DB_PASSWORD=${MYSQL_PASSWORD}
- WORDPRESS_DB_NAME=${MYSQL_DATABASE}
Inside the .env file, we need to valorize the variables for MySQL and HTTP port like this:
MYSQL_ROOT_PASSWORD=
MYSQL_DATABASE=
MYSQL_USER=
MYSQL_PASSWORD=
MYSQL_HOST=
HTTP_PORT=
And now start the Docker environment:
docker-compose up -d