Blog. Just a few musings.

Using Composer with WordPress

This is a simple guide to getting Composer to work with WordPress.

You will need to ensure your web server (development and production) are both running PHP 5.3.* minimum and will require the cURL extension to be enabled.

I develop and run all of my sites on a *nix environment (CentOS / Apache2 / PHP 5.4.*), so further instructions will only be valid for this platform. If you run on a Windows environment, please follow the instructions on Composer’s site.

Installing Composer

Open up a terminal window (SSH/directly on the machine) and run the following in your home directory.
cd ~
curl -s | php

We want to run composer globally, so next we will move it into your environment’s path.

sudo mv composer.phar /usr/local/bin/composer

Setting up Composer for WordPress

First navigate to your document root for your WordPress install.

Add a new file to the document root called composer.json.

touch composer.json

Open the file in your favourite editor (in this case, vi)

vi composer.json

In this article, I’ll provide an example of dependencies I have added to one of my recent projects which uses Doctrine2, Silex, Twig and a number of other libraries.

"require": {
"monolog/monolog": "1.2.*",
"doctrine/common": "2.3.*",
"doctrine/orm": "2.3.*",
"doctrine/dbal": "2.3.*",
"twig/twig": "1.*",
"sybio/image-workshop": "dev-master",
"silex/silex": "1.0.*",
"taluu/doctrine-orm-provider": "1.0.*",
"symfony/form": "2.1.*",
"symfony/validator": "2.1.*",
"symfony/config": "2.1.*",
"symfony/translation": "2.1.*",
"symfony/twig-bridge": "2.1.*",
"minimum-stability": "dev"

Save the file, and now we’re ready to download our dependencies. Run the following:
<Esc> :wq <Enter>
composer install

This process will download the libraries/framework to a new folder called ‘vendor’ in your document root. In addition to this, Composer will generate an autoloader for you to automatically include the files when they are called. The file is PSR-0 compliant and will load files by their namespace and class name.

To use the generated autoloader, open up your wp-config.php file in an editor and add the following:

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

/** Composer Autoloader */

Save the config file.

Now you should be able to use any of the libraries with a simple call to the class name in your plugins, themes, etc, eg.
$silex = new \Silex\Application();


  1. Tamlyn says:

    I may be missing something but isn’t this just how Composer works? What about it is specific to WordPress?

    Interesting that you mention Silex. Are you using it with WordPress?

    • Dean says:

      The only specificity to WordPress was pretty much adding the Composer autoload.php to wp-config.php.

      Silex wise … I’ll create a more specific tutorial in future. The short of it is to create a plugin class, setup a new Silex App in a bootloader (ie. with Doctrine, and other modules required) and ensure the $app variable is always in scope (be it in the WordPress Admin Menu / Menu hooks which use static classes, or through custom controllers). The only con I have found is that the Admin area’s use of non-seo friendly urls means manual routing using getRequest(), instead of Routing.

Leave a comment