Introducing third-party libraries in WordPress themes or plugins using PHP Composer
Composer is a package management tool for PHP, similar to Python's Pip or Nodejs' npm. With Composer, it's much easier for us to use certain PHP libraries in our PHP projects. WordPress is based on PHP, so it's logical to use Composer to manage PHP libraries used by WordPress themes and plugins.
PHP libraries that support Composer can be found in the Packagist.org As you can see above, this is the official repository for Composer. Before you use a PHP library, search for it on Packagist, and if it's there, you can use Composer to install and manage it.
In the following I will show you how to use Composer to manage PHP third-party libraries in WordPress themes or plugins, in the following introduction, we will use mailgun-php library to do the demonstration for you.
1. First, we need to install Composer
Just use the official command to install it, or if you are on Windows, you can download the official Composer-Setup.exe Installation package installation.
wpzhiku@ubutnu:~$ curl -sS https://getcomposer.org/installer | php
wpzhiku@ubutnu:~$ sudo mv composer.phar /usr/local/bin/composer2. Then add Mailgun as a dependency
We can add the libraries we need directly by using Composer's require command, which helps us download the library files and then generate the autoload file.
wpzhiku@ubutnu:~$ cd /path/to/plugin/
wpzhiku@ubutnu:~/path/to/plugin$ composer require mailgun/mailgun-php:~1.7.23. Check the composer.json file to see if it was added successfully.
After completing step 2, our composer.json configuration file should look something like this.
{
"name": "plugin-name/plugin",
"description": "Your plugin description",
"license": "proprietary", "authors": ["authors"], "homepage": "", "license": "proprietary", "keywords".
"authors": [
{
"name": "wpzhiku", "email":"", "authors": [ {
"homepage": "https://en.wpcio.com/"
}
], "type": "wordpress-plugin", "name".
"type": "wordpress-plugin", "require": { wordpress-plugin.net
"require": {
"php": ">=5.3.2", "require": {
"composer/installers": "v1.0.6", "mailgun/mailgun-php":"~1.0.6", "mailgun/mailgun-php".
"mailgun/mailgun-php":"~1.7.2",
"guzzlehttp/guzzle": "~5.2"
}
}4. Install Mailgun via Composer
If we have manually added the required libraries to our composer.json file, we need to run the following command to install the dependencies.
wpzhiku@ubutnu:~/path/to/plugin$ composer install5. Automatically load the Mailgun class in the plugin.
PHP has supported namespaces since version 5.3. If your PHP version is above 5.3, we can load the Mailgun class like this, writing the following code at the beginning of the plugin file.
require 'vendor/autoload.php';
use MailgunMailgun;Of course, we can also add the above code to our WordPress theme's functions.php to bring our installed libraries into our theme.
After completing this step, we can use Mailgun's function in the plugin or theme, how about it, isn't it very convenient?
Let's take a look at this.
Using composer to manage wordpress
http://levi.cg.am/archives/4771
Thanks for sharing, have subscribed to your blog.
Hello, your instruction installation is really hard to understand, I would like to ask how to introduce the installation if it is in a normal virtual host?
You can install it locally and upload it directly, you don't need to install it in your web host. As long as the web host's PHP version can meet the requirements, you can do it.