When you’re building several WordPress plugins or themes, you might come to realisation that you’re copying the same code over and over again. This might be classes for creating settings, libraries to change the whole admin area or it can be just a library that contains the whole logic behind updating your premium plugins.

In this tutorial, we will go over a Composer configuration which can help you retrieve that repetitive code and place it inside a folder of your plugin.

What is Composer?

I don’t want to spend too much on what is Composer but if you’ve never heard of it, it is a package manager for PHP libraries. This libraries can be placed on https://packagist.org/ or somewhere else, such as a GitHub repository.

Before following this tutorial, make sure that you have a Composer installed. I would recommend you to install it globally on your system so you can use it in every product.

By configuring Composer properly, you can import all the newest updates from those libraries with one command. Do note that some libraries can be used for production and some are for development-only.

Development-only libraries (or packages) are those that you don’t require in production for your product to work. Some common development-only packages are for testing purposes such as phpunit or for sniffing your code for style/code errors such as php_codesniffer.

Production packages (dependencies) are those that you require in production. You can leave them as they are inside the vendor folder that Composer will create for you. All packages will be installed inside that folder unless configured differently.

All packages installed through Composer, can be autoloaded with Composer also. In this tutorial, we will go over the classic approach of including your library directly because we will place it under a different folder.

The Library

This library can be anything you use over and over in your products. I will focus on Plugins but this can be used for Themes also.

My Plugin Library will be used to change the title of the site through a filter. Nothing special, but it will suffice enough to understand how this works 🙂

I will also make my library as a separate plugin. This will enable us to work only on the library without needing another plugin to activate it.

I’ll create a folder in wp-content/plugins and name it wp-plugin-library. I will also create a PHP file with the same name wp-plugin-library.php.

We are checking if our library already exists somewhere. If not, we will include our class. Inside of our main class, we check for a constant Plugin_Library_Loaded. If that exists, it means that our plugin library was already loaded. This is useful, if you have some initial setups that should run only once.

If you don’t have to run initial setups such as enqueueing scripts and styles, you don’t need this. For example, we don’t want to filter the site title more than once since we will have the same outcome.

We are also hooking onto init and load our library if we did not load it through one of our plugins.

The Library’s Composer.json

We need to configure the composer.json of our library so that it can be used in our products. You can define the composer.json only for development, but here we want this library to be used elsewhere.

Since our library is something that we want to drop into another project, I have defined its type as wordpress-dropin. The require-dev packages are development-only packages. We don’t need to include phpunit or codesniffer into other products because we are using those only while developing our library.

But we do need composer/installers because we want to enable other products to define where to install our library. Without that package, other products will only be able to install our library inside the vendor folder.

You can check the library example here: https://github.com/igorbenic/wp-plugin-library.

The Product

Our product here will be an example on including our library and loading it. Nothing else. In a real word example, your product will probably be a good complex plugin or theme since it is using libraries 🙂

My product will be a plugin and the folder name is: wp-plugin-product. Also, the file inside of that folder has the same name wp-plugin-product.php.

We are including our plugin library and when all the plugins have been loaded, we are loading our library. Before looking into composer.json create another folder inc. We will include the library inside that folder.

The Product’s Composer.json

For our product, we need to define a composer that will know where to retrieve our library, how to retrieve it (is it production or development-only) and where to put it.

We have defined that there is a version-control repository where are library is placed. We have also defined that we need that for production and inside the extra we have defined where to put the package igorbenic/wp-plugin-library (the name inside the library’s composer.json).

Now when everything is saved, you need to go to the product’s folder through terminal (command prompt) and then type composer update. This will install the library inside the inc folder.

Conclusion

I hope, you have a much better understanding on how you can use Composer to reuse the code you already reuse in your products. You can also try this without the extra parameter inside of product’s composer.json and then include the loader vendor/autoload.php.

Have you tried using Composer in your WordPress Products? Were there any issues using it? Share with us in the comments below.

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

4 Comments

  1. Thanks for sharing i like nice

    Reply

  2. Apologies in advance if I am missing something. I am working on a plugin that needs to require several APIs to be installed via composer within the plugin. Users of the plugin should not need to install composer or use the terminal to update dependancies. Suggestions?

    Reply

    1. Hi Meg, the dependencies will need to be installed (updated) prior to deploying this plugin. When you create a ZIP of the plugin, the installer should be run first (but only for dependencies, not dev-dependencies) or if you push this plugin to the wp.org repository. Before pushing it there, run the composer.

      Reply

Leave a reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.