Using Composer with WordPress
WordPress Packagist
The official WordPress plugins and themes directories do not support Composer. To enable management of plugins and themes via Composer, we can use WordPress Packagist, a third party repository that mirrors the WordPress plugin and theme directories as a Composer repository.
How do I use it?
- Add the WordPress Packagist repository to the repositories section of your
composer.json
- Add the desired plugins and themes to your requirements using
wpackagist-plugin
orwpackagist-theme
as the vendor name.
Example composer.json
:
{
"name": "acme/brilliant-wordpress-site",
"description": "My brilliant WordPress site",
"repositories": [{
"type": "composer",
"url": "https://wpackagist.org",
"only": [
"wpackagist-plugin/*",
"wpackagist-theme/*"
]
}],
"require": {
"aws/aws-sdk-php": "*",
"wpackagist-plugin/akismet": "dev-trunk",
"wpackagist-plugin/wordpress-seo": ">=7.0.2",
"wpackagist-theme/hueman": "*"
},
"autoload": {
"psr-0": {
"Acme": "src/"
}
},
"extra": {
"installer-paths": {
"wp-content/mu-plugins/{$name}/": [
"wpackagist-plugin/akismet"
],
"wp-content/plugins/{$name}/": [
"type:wordpress-plugin"
]
}
}
}
This example composer.json
file adds the WordPress Packagist repository and includes the latest version of Akismet (installed as a must-use plugin), at least version 3.9 of Captcha, and the latest Hueman theme along with the Amazon Web Services SDK from the main Packagist repository.
Bedrock
Bedrock is a modern WordPress stack that helps you get started with the best development tools and project structure.
Much of the philosophy behind Bedrock is inspired by the Twelve-Factor App methodology including the WordPress specific version.
Bedrock Features
- Better folder structure
- Dependency management with Composer
- Easy WordPress configuration with environment specific files
- Environment variables with Dotenv
- Autoloader for mu-plugins (use regular plugins as mu-plugins)
- Enhanced security (separated web root and secure passwords with wp-password-bcrypt)
Bedrock Requirements
- PHP >= 7.4
- Composer (Installation)
Getting Started
- Create a new project:
bash $ composer create-project roots/bedrock
- Update environment variables in the
.env
file (ignore this file in git): - Database variables
DB_NAME
- Database nameDB_USER
- Database userDB_PASSWORD
- Database passwordDB_HOST
- Database host- Optionally, you can define
DATABASE_URL
for using a DSN instead of using the variables above (e.g.mysql://user:password@127.0.0.1:3306/db_name
)
WP_ENV
- Set to environment (development
,staging
,production
)WP_HOME
- Full URL to WordPress home (https://example.com)WP_SITEURL
- Full URL to WordPress including subdirectory (https://example.com/wp)AUTH_KEY
,SECURE_AUTH_KEY
,LOGGED_IN_KEY
,NONCE_KEY
,AUTH_SALT
,SECURE_AUTH_SALT
,LOGGED_IN_SALT
,NONCE_SALT
- Generate with wp-cli-dotenv-command
- Generate with our WordPress salts generator
- Add theme(s) in
web/app/themes/
as you would for a normal WordPress site - Set the document root on your webserver to Bedrock's
web
folder:/path/to/site/web/
- Access WordPress admin at
https://example.com/wp/wp-admin/
Multisite
Bedrock is multisite network compatible, but needs the roots/multisite-url-fixer mu-plugin on subdomain installs to make sure admin URLs function properly. This plugin is not needed on subdirectory installs but will work well with them. From your Bedrock directory:
$ composer require roots/multisite-url-fixer
Bedrock Documentation
The full Bedrock documentation is available at https://roots.io/bedrock/docs/.