As a WordPress developer, I do often rely on Composer libraries to easily manage CPTs, Settings, Infinite scroll, API integration, and much more…

Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

getcomposer.org

Beautiful definition, but what does it mean?

We can see Composer like a kind of npm but for the PHP world, and like npm, it allows you to easily install, update and remove 3rd party libraries, or publishing your own private/public ones.

Let’s have a look

When writing a theme or plug-in for WordPress quite often we need to register our CPT, and here we have two possibilities:

1. Use the built-in register_post_type function

2. Use a 3rd party class like PostTypes

While the first option works fine, we can argue that with the second one we:

  • have a more clear syntax
  • can easily attach taxonomies to the CPT
  • can easily customise the columns in the back-end.

So, let’s see how to use that library with and without Composer

The manual way

In this case, we want to put our 3rd party libraries in a separate folder, for example, /lib, so:

1. Create the folder

That’s easy; let’s create one:

    mkdir lib

2. Download the package

Let’s open the GitHub link and download the zip

3. Unzip

Now we can unzip

To include 3rd party library we could y create a subfolder, for example, /lib, copy inside the libraries we want to use and include it.

4. Include the library

In our case, we have to add the following line in our theme/plugin:

    require 'lib/PostTypes/src/PostTypes.php';

That’s it, it works!

Finish

Although manually installing a library is relatively easy to job to do, it soon becomes tedious to install, update or remove additional libraries.

So, as you can see, this is not convenient that convenient after all.

The Composer way

Assuming that we already have installed Composer, all we need to do now is:

1. Install the library

open the terminal in our project folder and run:

    composer require jjgrainger/posttypes

2. Import the autoload.php file

In your functions.php or the plugin index.php file, add the following line:

    require __DIR__ . '/vendor/autoload.php';

NOTE: This step is only required once, so we don’t need to do anything when installing additional libraries

That’s all

As seen here, Composer is very easy to use, and also we have to learn only a few commands to master it.

Why I do prefer Composer?

Although we did have a look at a simple example, in reality, I do often need reusing code across multiple projects or interact with 3rd party APIs.

In those cases, I don’t want to keep reinventing the wheel every time, because:

  1. it is a bug-prone process, the more code we write more likely we’re introducing new bugs
  2. we have more code to test and maintain

For this reason, I’ve created my own libraries to handle common problems I face quite often during the development, like:

  • load more and dynamic filtering with ajax
  • process large dataset and files in batches
  • manage setting pages
  • helpers for debug information (useful during development)

So while working on a plugin or theme, I can add any of them with a simple composer require command.

Other benefits are:

  • any time I fix a bug or implement a new feature in any of those libraries, I can easily update them in my projects by using composer update

  • I can easily share them so that can be easily installed via Composer

  • I can specify which version of the package I want to install.

    For example, the WordPress test library is not compatible yet with the latest PHPUnit (8.x), so for writing unit test we have to specify which version to install:

  composer require phpunit/[email protected]
  • The lock file

    Every time we install, update or remove a library Composer will generate/update a file called composer.lock.

    This file contains information about the version of the library we’re using, so next time we run a composer install, it will make sure to use the same version.

  • The repository Packagist is the main Composer repository, and at the moment, there are more than 2.8k libraries with the tag WordPress.

As you can see for me all this flexibility is vital for my development process.

What’s next?

In the next articles, we’re going to talk more about Composer, and starting from the next one we’re going to see how to publish and use our private and public library.