Using Composer to simplify WordPress dependencies

0
(0)

In this post I’m going to explain how to use the Composer dependency manager in your WordPress plugins to simplify using additional modules in your code.

Adding additional modules to wordpress

When you are developing a WordPress plugin it’s often the case that you need to use additional modules to make your task simpler.

As a case in point, I’ve recently been working on a plugin to share Posts and Custom Post Types on my blogs to social media via a regular cron.

This is not a difficult task, but there are some modules I needed to make this work – for example there are PHP SDKs for both Twitter and Facebook which deal with authorization and posting to pages etc, and I wanted to use Guzzle for the communication necessary to create access tokens for Facebook.

At first I just downloaded the modules as zip archives and included them in my plugin directory, but a bit later I wanted to include the twig templating system for a dashboard widget and I decided it would be better to have everything controlled by composer rather than being installed as a separate modules.

Using Composer to control additional modules

The main reasons for using composer are that you simplify the whole dependency problems that you can get into if you work with additional modules.

For example I mentioned above that I wanted to use Guzzle for http communication. I haven’t looked, but its quite likely that both the twitter SDK and the Facebook SDK also use Guzzle for their communications because it’s a popular module.

If I just download the individual modules it’s likely that one of two things will happen. Either both modules have included Guzzle in their downloadable packages, so I end up with two copies, or neither have included it and I end up with a module which doesn’t work until I add Guzzle.

And it isn’t just Guzzle – there are likely several common modules which are used in those two SDKs and each of them would need to be installed separately to ensure everything worked.

This is the problem which Composer solves; I don’t need to worry about the individual requirements of each module I want to use. I just issue a simple command and it takes care of downloading and installing the module and every dependency that module has. In fact, in the situation I’ve just described, if I want to use Guzzle it will be the same copy of Guzzle which is used by the Facebook and Twitter modules as well.

Installing and using Composer

If you don’t have composer installed on your development setup, it’s just a case of heading over to the main site and running the command listed there to install it.

Once it is installed, you need to create a directory in your plugin to store the modules which will be downloaded and setup when you request them. I tend to create a ‘lib’ directory in my plugin directory for this purpose.

cd PLUGINDIR
mkdir lib
cd lib

To install a module you just need to tell composer to install it. The command for this, which is run in the lib directory just created is,

composer require guzzlehttp/guzzle

Once you run that command, composer will look at the guzzle package, find what dependencies it has and then download both guzzle and its dependencies into a directory called ‘vendor’.

Once all the code is all in place composer will create an autoload file for you which will autoload all the classes needed for the modules to run correctly. The only thing you need to do is add that autoloader into your plugin code.

I normally have a single php file which sets up my plugin with the required WordPress header information and an autoload function which will add all my own code. All that needs to change is to add the composer autoload to that file

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

The good thing is that when I want to add any additional modules I just need to cd to the lib directory and add them

cd PLUGINDIR/lib
composer require twig/twig

When I run this command, composer looks at the requirements that twig has and works out what it will need to download to satisfy both twig and every other package you have installed.

Composer created files

You will find that once you run a composer require command in your ‘lib’ directory that there is another file which has been created called composer.json.

This is the file which lists the packages you have requested and is the way that composer keeps track of those packages.

There is also another file called composer.lock which is used to track the versions of the different packages which have been installed.

Finding Composer Packages

You will have noticed that the composer require commands need to have a specific name for the package – you can’t just do ‘composer require twig’. So how do you know which name to use?

Well the best place to look for these is Packagist. This site lists thousands of php modules which all work with composer and can be searched for the package you need.

One other point to mention is that this article has only touched the surface of composer dependency management. You can specify exactly which version of a package you need, add private composer repositories etc. Maybe I’ll cover that in a different post later.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Please consider sharing on social media!

Leave a Reply

Your email address will not be published. Required fields are marked *

An Advanced Custom Fields form showing the Conditional Logic selector Previous post A Useful Advanced Custom Fields Conditional Logic trick.
Vagrant Development Environment for Wordpress Next post How to easily set up Vagrant to develop on WordPress