Using Composer Dependencies in WordPress

Composer

In this tutorial, I’m going to show you how you can add Composer Dependencies to your WordPress theme.

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.  (https://getcomposer.org/doc/00-intro.md)

Getting Started

The first thing you’ll need are a couple of items, actually — a working WordPress install, a basic theme with functions.php, and Composer. After you get these prerequisites installed, you’re ready to started using Composer with WordPress.

Go ahead and create a composer.json file in the theme’s root. This file, composer.json, informs Composer of which libraries it needs to install. After this is done, add some dependencies to it; here’s a sample of what you could install — because of using PHP itself, I highly recommend adding a Logger mechanism to your theme, so that’s what we’ll do in this tutorial.

 // in wp-content/themes/THEMENAME/composer.json
{
    "require": {
        "monolog/monolog": "1.0.*"
    }
}

After you have the dependencies you want to use added, go ahead and run composer install. This will create a vendor directory within the theme.

Autoloading The New Dependencies

After you’ve ran composer install, it’s time to autoload the new libraries which are located in vendor. Open up the theme’s functions.php file and require_once the autoload.php file like this:

// in wp-content/themes/THEMENAME/functions.php

// __DIR__ can be used if you have PHP 5.3+
require_once( dirname(__FILE__) . "/vendor/autoload.php" );

Honestly, I could stop the tutorial here because I’m now to the point that I can begin using the dependencies; for those that are unsure yet, I’ll show how you can use this with Monolog in order to log whenever the administration section (wp-admin) is accessed.

Logging When wp-admin is Accessed

In order to log an event like accessing the admin, we need to know what actions fire when the admin section is accessed. If you look through the WordPress Codex, you’ll find one such action: admin_init. So let’s hook that action and tell Monolog to log the current request to THEMENAME.log.

// in wp-content/themes/THEMENAME/functions.php

// __DIR__ can be used if you have PHP 5.3+
require_once( dirname(__FILE__) . "/vendor/autoload.php" ); 

add_action("admin_init", function(){

    // "MyLog" is the name of the log's channel.
    $logger = new \Monolog\Logger("MyLog"); 

    // add a stream handler that logs to THEMENAME.log. The Minimum level to record information is DEBUG.
    $logger->pushHandler(new \Monolog\Handler\StreamHandler(__DIR__ . '/THEMENAME.log', \Monolog\Logger::DEBUG)); 

    // log all the _SERVER variables 
    $logger->addInfo( "The Admin Section Has Been Accessed: ", $_SERVER ); 
});

And it’s really that simple! If you browse to wp-admin, you should see new information in the log detailing the _SERVER’s information.

Leave a Reply

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

Thanks for your thoughts!

*