PRO TIP: Using Composer to Manage Plugins

Composer is a command-line tool to manage 3rd party PHP libraries. But not many people know that it can be used for WordPress plugins and themes.

Composer is a command-line tool to manage 3rd party PHP libraries. But not many people know that it can be used for WordPress plugins and themes.

Follow along this tutorial to learn how:

NOTE: This guide is for Windows using WAMP Server. If you use Mac or other server, the installation part vary a bit.

1. Installation

First, install WAMP Server and Composer:

Check if the Composer has been successfully installed by running the command composer -V.

2. Update PHP Version

Check your PHP version by running the command php -v. If it says 7.0 or above, you can skip this step.

If it’s below 7.0, update the PHP version by following these steps:

  1. Go to C:\wamp\bin\php and copy the folder path of the PHP versions that you want. For example C:\wamp\bin\php\php7.2.14.
  2. Open Environment variables dialog.
    How? Press Windows button > type “env” > click “Edit environment variables for your account”
  3. Double click “Path” under User Variables.
  4. Replace the path to PHP folder with your new one.
  5. Done! Check the version again by running php -v.
Changing PHP version in Environment Variables

3. Create Composer Config File

Create composer.json file in the root directory of your WordPress site. It contains a list of plugins and themes.

Example:

{
  "name": "my/demo",
  "require": {
    "wpackagist-plugin/timber-library": "~1.13.0",
    "wpackagist-plugin/autodescription": "~4.0",
    "wpackagist-plugin/contact-form-7": "~5.1",
    "wpackagist-plugin/w3-total-cache": "*",
    "wpackagist-theme/twentytwenty": "*"
  },
  "repositories": [
    { "type": "composer", "url":"https://wpackagist.org" }
  ],
  "autoload": { "psr-0": { "Acme": "src/" } }
}
  • (Line 4) The tilde (~) symbol means the last digit can be updated. So if Timber releases version 1.13.1, it will use that. But when 1.14.0 is released, it will stay at 1.13.1.
  • (Line 7) The asterisk (*) symbol means to get the latest version.
  • (Line 8) To download theme, use the prefix wpackagist-theme/.

For plugins that are available in WordPress.org, you simply add wpackagist-plugin/$slug. The slug can be seen from the URL of that plugin’s page.

For custom plugins, jump to Step 5.

4. Download the Plugins & Themes

Run this command in the root directory of your site:

composer update

HINT: You can type “cmd” in the File Explorer’s address bar to open command prompt in that directory.

Downloading WP plugins with Composer

Done! If you want to update the plugins, simply run the same command.

5. Custom Plugins

For plugins that are not in the official repository, there are 2 ways:

  1. If you are the creator, put it on Github and register it in Packagist with type wordpress-plugin.
  2. If you are not the creator, zip the plugin (should only contain 1 folder), upload it somewhere, and link it in the config like shown below:
{
  "name": "my/demo",
  "require": {
    "custom/my-plugin",
    ...
  },
  "repositories": [
    { "type": "composer", "url":"https://wpackagist.org" },
    { "type": "package", "package": {
      "name": "custom/my-plugin",
      "version": "1.0.0",
      "type": "wordpress-plugin",
      "dist": {
        "type": "zip",
        "url": "https://yoursite.com/files/my-plugin.zip"
      },
      "require" : { "composer/installers": "~1.0" }
    } }
  ],
  "autoload": { "psr-0": { "Acme": "src/" } }
}

Conclusion

There are many benefits of using Composer:

  • Convenient – One command to get everything.
  • Synchronize – Making sure your Live and Local site uses the same plugins and versions. (You need SSH access to install Composer in your live server).
  • Git Friendly – Commit composer.json and ignore wp-content/plugins.

For more power, you can combine Composer with WP-CLI so you can download the WP core files too.

Let me know in the comment below if you have trouble in the comment below. 🙂

Default image
Henner Setyono
A web developer who mainly build custom theme for WordPress since 2013. Young enough to never had to deal with using Table as layout.
Leave a Reply