How to install Wordpress plugins and themes with Composer

Goal

Add external plugins or themes from the official directory to your Wordpress installation using composer.

Assumptions

Problems

Since Platform.sh deploys the code read-only (and thus makes many Wordpress sites more secure by default), the integrated mechanism to add and update both plugins and themes doesn’t work.

Luckily, wpackagist.org mirrors the WordPress plugin and theme directories as a Composer repository. This means you can add all plugins and themes covered by the directory using composer.

Steps

1. Add the external repository

Edit your composer.json file and add wpackagist as a repository.

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

2. Add a plugin or theme via composer

You can use composer require to add a plugin or a theme as a dependency. For plugins, use wpackagist-plugin as the vendor name, for themes use wpackagist-theme.

Examples:

Plugin: composer require wpackagist-plugin/cache-control

Theme: composer require wpackagist-theme/neve

Composer will update your composer.json and composer.lock files accordingly.

3. Push to your repository

By pushing those changes to Platform.sh, the build process will automatically install the themes and plugins in the right folder.

git add composer.json composer.lock
git commit -m 'adding themes/plugins'
git push

4. Enable plugins/themes in the WP-Admin Dashboard

The admin interface will show the plugins/themes and will allow you to enable them directly via the interface.

57
05

Conclusion

By using composer to install 3rd party plugins and themes, you get a reliable build process and make sure the code is deployed safely. This avoids committing plugins and themes into your git repository.

It also makes it easy to keep plugins and themes up to date. You can follow this guide on how to keep your Wordpress site updated.

1 Like