PHP, Software Development

WordPress Plugin And Composer

This post quickly shows the basic usage of WordPress Plugin and Composer. We are to use a PHP library within our own already existing WordPress Plugin.

Composer

We can use Composer with a WordPress plugin. The composer is a dependency manager for PHP. Before using it, we need to install it. Please see the Getting Started page. Depending on our operating system, go through the appropriate installation guide. Then, we will use the PHP library random-lib.

Run the following command in Windows within some directory.

The command will create a vendor directory and composer.json and composer.lock files.

The structure of the vendor directory is as follows. Later, we will copy the vendor directory created by Composer to our WordPress Plugin.

WordPress Plugin

As stated earlier, our plugin is an existing WordPress plugin that is still in the early development stage. We just want to use a PHP library in a WordPress Plugin.

With the PHP library downloaded, copy the vendor directory to a subdirectory within our WordPress Plugin location.

Then, we need to include the file autoload.php into our WordPress Plugin main PHP file.

The following are sample codes using the library. The variable $randomString will contain a random String value of 10 characters using a, b, c, d, e, and f.

Please note secure coding is not taken into consideration in this post.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like