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 directions 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 mirror of all plugins and themes which you can download from wordpress.org. You can add any custom repositories you have here, 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 our minimum needed PHP version and 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 clever plugin composer/installers which allows us to have custom installation path. In Extra section we can define installation paths for different types of packages.

Adding your own plugins and themes

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 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"
}

then requiring it in your project like:

{
  "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.

More information

You can find more documentation on getcomposer.org