Atomic Smash homepage splash

Using Composer in WordPress development

Words by Adam WardSeptember 17, 2016

In this post, I’ll will give a brief overview of what Composer is and how you can start using it in your projects. I will also run through an example of how we combine Composer into our WordPress development at Atomic Smash.

First, lets have a look at Composer…

Composer is a PHP dependency management tool. This means it can manage any packages, plugins, libraries, themes etc that you require for your project.

Why is this helpful?

Imagine you are working on a project and pushing your code to a repository, you don’t want to also store all of your projects dependencies in your repo too. This is where Composer comes in – it allows you to declare all of your dependencies in one file. When another Developer starts to work on your project, all they need to do is clone your repo and run Composer on their computer to install all of the dependencies required and at the versions you specify for your project.

This can seem a little confusing at first but it’s almost certainly something you will require and encounter at some point as a Developer. I’ll run through an example and hopefully the advantages of using Composer will become much clearer.

Lets make a Composer example project…

In order to start using Composer in your project, it must first be installed on your machine – the Composer doc’s do a good job of explaining how to get started – getcomposer.org

To use Composer in your project, you first need to create a ‘composer.json’ file in the root of your directory. This JSON file is where you declare what dependencies you require and even the version number.

# Make a new directory and cd in to it
$ mkdir project-example && cd project-example

# Create your composer.json file
$ touch composer.json

If you open the composer.json file we just created in your text editor you can start to list out the dependencies your project requires. Here’s an example of how to do this…

{
    "name"        : "example-project",
    "description" : "Composer file for my amazing website",
    "authors"     : [
        {
            "name"    : "Adam Ward",
            "email"   : "adam@atomicsmash.co.uk",
            "homepage": "http://www.example-project.com/"
        }
    ],

    "require"     : {
        "monolog/monolog" : ">=1.20.0"
    }
  
}

Monolog is a example of a PHP package we are declaring to Composer, that we need for this project. Monolog is a package that sends your logs to files, sockets, inboxes, databases and various web services. You can find more packages at https://packagist.org/explore/

Now we are ready to install Composer within our project. Head back to your Terminal window and enter the following…

# Make sure you're in the project-example directory
$ composer install

Composer will now run in your terminal window and install any of the packages we have declared within our composer.json file. Great! We’re up and running with Composer. Now hopefully you have a better understanding of how Composer works, lets have a look at how to use it with WordPress.

Composer + WordPress

I am assuming that you’re already comfortable with WordPress and how the file structure works. There’s a couple of key things to remember when working with Composer –

Composer can only install dependencies into a project if they have a composer.json file. This is an issue, as most of the plugins in the WordPress plugin directory do not have a composer.json file. Luckily, the team at Outlandish have created a mirror of the WordPress Plugin directory called https://wpackagist.org/, which means we can declare any WordPress plugin in the official plugin directory.

Another thing to remember, WordPress itself can be installed via Composer and declared as a dependency. WordPress also doesn’t have a composer.json file in it’s core, which means we need a workaround for this too. There’s a few WordPress clones already out there with a composer.json file added, probably the most common of these and the one we use at Atomic Smash is https://github.com/johnpbloch/wordpress.

It’s become fairly common practice to install WordPress in a sub directory. Not everyone prefers this method but I find it much easier to keep the core WordPress files separate from my theme files. This method does mean you need to make a couple of changes to your root directory, which I won’t go into in this post but if you do prefer installing WordPress in it’s own directory, you’ll need to add the ‘extra’ declaration, as shown in the example below, which tells Composer where to install WordPress.

Below is an updated version of our composer.json file, which is set up to work using the methods above.

{
    "name"        : "example-project",
    "description" : "Composer file for my amazing website",
    "authors"     : [
        {
            "name"    : "Adam Ward",
            "email"   : "adam@atomicsmash.co.uk",
            "homepage": "http://www.example-project.com/"
        }
    ],

   "type"        : "project",
    "repositories": [
        {
            "type": "composer",
            "url" : "http://wpackagist.org"
        }
    ],
    "config"      : {
        "vendor-dir": "vendor",
        "secure-http" : false
    },
    "require"     : {
        "johnpbloch/wordpress" : ">=4.2",
	"wpackagist-plugin/wordpress-seo" : "*",
	"wpackagist-plugin/simple-page-ordering" : "*",
        "wpackagist-plugin/wp-mail-smtp" : "*"
    },
    "extra"       : {
        "wordpress-install-dir" : "wp"
    }
  
}

Here’s a breakdown of what we’re doing in the above example –

  1. Giving our file a name, description and listing Author info
  2. Declaring the ‘wpackagist’ repository, which enables us to require any of it’s plugins
  3. In ‘config’ we are declaring which directory the vendor files are kept
  4. We ‘require’ WordPress and the plugins we want and also the version numbers ( * = latest version)
  5. Lastly, ‘Extra’ is where we tell Composer to install WordPress in sub directory

Wrapping up

Depending on your experience with WordPress and whether or not you’re familiar with Composer, it could feel like there’s a huge amount to take in from this post. Even if you feel like this method is a bit overkill for your current setup/project, it’s still worth getting to grips with Composer and trying it out in a couple of projects as it’s very likely you’ll need it in the future.

Profile picture of Adam Ward

Adam WardDeveloper

Adam is an expert in WordPress development and CRM integrations. His ability to craft WordPress to a users needs makes our websites easier to use.

Go back to top

Keep up to date with Atomic news