How to use Composer packages in Wordpress

Published on Jul 9, 2013

I recently had the need to use a couple of Composer packages in a Wordpress project (Carbon, Calendr, Illuminate Support...). This is actually really easy to achieve. First of all you're gonna need to setup your composer.json file with all the dependencies. Feel free to go wild and use whatever you want :)

Since Composer has an autoloader built in, we can also use it to autoload some of our custom classes. Was actually building an Event manager, but none of the plugins had all the features, so I built my own classes with the help of the packages I mentioned above. These custom classes are in the directory "wp-content/themes/my_theme/lib", so my composer.json file looks like this:

	"require": {
		"illuminate/support": "4.0.*",
		"nesbot/Carbon": "*",
		"imagine/Imagine": "v0.4.1",
		"yohang/calendr": "1.*"
	"autoload": {
		"classmap": [
	"config": {
		"preferred-install": "dist"
	"minimum-stability": "dev"

I assume you already have Composer setup on your machine, if not go ahead and do that first:

Then just run "composer install", and all the dependencies will be installed inside a directory called "vendor". But to actually use the packages in our theme we need one final step and that involves editing the "wp-config.php" file. You could put it inside "index.php" or even "functions.php" in your theme, but I did it inside my config file.

require_once(__DIR__ . '/vendor/autoload.php');

And this is it, you can now use the Composer packages in your theme, just keep in mind that those classes are namespaced, so if you want to use the Carbon class, you need to call it like this:


Or if you're using it inside your own custom class like me (I have a class called Event), you need a "use" statement before you declare the class:


use Carbon\Carbon;
use CalendR\Event\AbstractEvent;

class Event extends AbstractEvent {

	public function someMethod()
    	$now = Carbon::now();


And this is it ;)