Fork me on GitHub

Summary: Seravo uses Composer by default to install plugins and themes. You don’t have to use it, but it’s pre-installed for you in case you want to have better management of your PHP code dependencies.

What is it?

Composer is a dependency manager for PHP that has been gaining popularity lately. Your first question is most likely “what is a dependency manager and why do I need one?”. Almost any code you write probably ends up depending on 3rd party libraries. All of these libraries (projects, frameworks, files, etc) become dependencies of your project. Composer lets you declare the dependencies for a project and it will install and manage them.

Source: roots.io

How to use Composer with WordPress

Example

Let’s look at simplified version of our composer.json as an example:

{
  "repositories": [
    {
      "type": "composer",
      "url": "http://wpackagist.org"
    }
  ],
  "require": {
    "php": ">=5.3.2",

    "johnpbloch/wordpress": "*",

    "wpackagist-plugin/wordpress-seo": "*",

    "wpackagist-theme/twentyfifteen": "*"
  },
  "extra": {
    "installer-paths": {
      "htdocs/wp-content/plugins/{$name}/": ["type:wordpress-plugin"],
      "htdocs/wp-content/mu-plugins/{$name}/": ["type:wordpress-muplugin"],
      "htdocs/wp-content/themes/{$name}": ["type:wordpress-theme"]
    },
    "wordpress-install-dir": "htdocs/wordpress"
  }
}

Explanation

Repositories section:

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

Repositories section tells Composer where to look for your packages. In order for Composer to find your beloved WordPress plugins and themes you’ll need to point it to wpackagist.org, which is a mirror of all the plugins and themes that you can download from wordpress.org. Here you can add any custom repositories you may have, but for WordPress you’ll need to use wpackagist.

Require section:

{
  "require": {
    "php": ">=5.3.2",

    "johnpbloch/wordpress": "*",

    "wpackagist-plugin/wordpress-seo": "*",

    "wpackagist-theme/twentyfifteen": "*"
  }
}

Require section tells Composer the minimum PHP version needed and all the packages to install.

"johnpbloch/wordpress": "*" means newest available version of WordPress.

"wpackagist-plugin/wordpress-seo": "*" means that we need plugin WordPress Seo

"wpackagist-theme/twentyfifteen": "*" means that we need theme Twentyfifteen

Extra section:

{
  "extra": {
    "installer-paths": {
      "htdocs/wp-content/plugins/{$name}/": ["type:wordpress-plugin"],
      "htdocs/wp-content/mu-plugins/{$name}/": ["type:wordpress-muplugin"],
      "htdocs/wp-content/themes/{$name}": ["type:wordpress-theme"]
    },
    "wordpress-install-dir": "htdocs/wordpress"
  }
}

By default Composer installs everything to vendor directory in the root of your project. Luckily for us wpackagist uses a clever plugin composer/installers that allows us to have custom installation paths. In Extra section we can define installation paths for different types of packages.

Adding your own plugins and themes

The best way to develop custom plugins and themes is to add them into their own repositories and install them by Composer.

You can do this by adding a composer.json file for into your plugin/theme repo:

{
  "name": "your-name/custom-plugin",
  "type": "wordpress-plugin",
  "license": "GPLv3",
  "description": "Plugin description",
  "homepage": "https://github.com/your-name/custom-plugin"
}

…and then requiring it in your project like this:

{
  "repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/your-name/custom-plugin.git"
    }
  ],
  "require": {
      "your-name/custom-plugin": "*"
  }
}

This way you can use plugins and themes from Github or Bitbucket.

By default Composer only fetches modules that are publicly available on Github. For paid WordPress plugins that are not freely available, there are basically two alternative approaches. Either you can include them in our project repository and distribute them together with the project code, or you can get a paid account from Packagist.com that includes the ability to have private Composer packages.

More information

You can find more documentation on getcomposer.org