Before starting the tutorial, it must be clarified that I am referring to Composer, the famous PHP dependency manager used in frameworks such as Laravel, and it has nothing to do with Visual Composer (a WordPress plugin for visual layout).

Now that it is clear we will not be discussing Visual Composer, let us begin with the basics.
What is Composer?
Composer is a tool for dependency management in PHP. It allows us, as programmers, to declare the libraries on which our projects depend, and Composer will manage them (install / update).
What is the purpose of Composer in a WordPress plugin?
Imagine you are developing a WordPress plugin and you have several PHP libraries such as CMB2: Custom Meta Boxes, Advanced Custom Fields, and some libraries for generating PDFs or Excel files, PHPUnit, etc. The most common way to integrate these libraries is to go to the official website, download the latest version, and then copy them into your plugin folder. But...
Can you imagine that in the future you need to maintain the plugin, or perhaps one of the libraries you used in the plugin is no longer compatible with the server's PHP version and you need to update the library? Well, Composer saves us from many of these types of problems, since through our command console we can update all libraries and even install them all, without needing to download them one by one, copying and pasting.
How to install Composer?

Open a new tab in your browser and search on Google.

We will type composer and go to the first Google result, making sure the website address is: https://getcomposer.org.


We will see a new page and click on Composer-Setup.exe. Once the download of the .exe file is complete, we must run it. It will request some administrator permissions; we will click accept.

We must click on the “Next” button.

Next, it asks us to indicate the path to the PHP executable. In my case, since I am working with:
- Laragon, the path is C:\laragon\bin\php\php-version\php.exe
If you are using a local server such as:
- XAMPP, the path is C:\xampp\php\
- WAMP, the path is C:\wamp\bin\php\php
NOTE: You must select the php.exe file, then click “Next”.

We will click “Next”.

Click “Install”.
Once everything is installed, other windows may appear where you simply need to click Next, and then Finish; after so many Nexts, the Composer installer will have added the PHP folder path and its own Composer folder to our global PATH.
This allows us to work from the console by simply typing php or composer without needing to specify the executable path. To verify that everything is in order, we will perform two small tests, so it is time to open the console and type:
php -v (press Enter)
To confirm everything is correct, we should see the version of each, as shown in the following image:

With this, we now have Composer installed and running on Windows. Now we just need to test Composer in our plugin.
How to use Composer in a WordPress plugin?
First, we must go to our WordPress plugin folder. In my case, my plugin is located at the following path: \wp-content\plugins\demo_composer, Inside this folder, we should have the main plugin file. In my case, I created a file for demonstration purposes only:
<?php
Next, we will open the path to our plugin using the command line. In Windows, simply right-click on the plugin folder and click “Open in Terminal”.

Our Windows command terminal will appear. Simply type the following line to use Composer in our WordPress plugin:.
composer init

We will press “Enter” or the “N” key to skip the initial configuration.

If it asks us to define our dependencies, we will type no in both cases. Then it will ask if we agree with the generation; we will type “yes” or press the “Enter” key, and Composer will generate a file in .json format called composer.json in our project.

Now we can proceed to install a library. We can search for many on Packagist In this case, I will install the library spipu/html2pdf for my plugin to generate PDFs. This library is installed with the following command:
composer require spipu/html2pdf


Once the command is executed, we will see a screen like the previous one, and a folder named vendor will have been created in our project.

To use the libraries installed with Composer and saved in the vendor folder composer within our WordPress plugin, vendor, we only need to include the «autoloader» located inside the vendor folder, which will handle including all dependencies in the main plugin file.
<?php;
With just the line of code that includes the file autoload.php within our plugin, we will have integrated Composer and all the PHP libraries you want, since that file handles everything.
Once the “vendor” file is included, simply activate our plugin and program the corresponding code to use our library in the plugin.
Still have doubts?
Do you need help with a web project? Don't hesitate to contact me. I develop complete and customized solutions with WordPress and PHP, using modern tools and processes, HTML, CSS, SCSS, PHP, JavaScript, Bootstrap, and more. Ready? Send me a message and let's talk about your web project!
