Utilizing Composer within your WordPress Application

Last Updated -


Please note beyond showing you how to utilize this tool within our environment Pagely cannot troubleshoot it in your existing application, nor can we mitigate the added risk of this breaking your site should the dependency be inappropriately removed. It is up to the developer to ensure composer is used appropriately within their Application.


WordPress plugins can add a lot of additional functionality to your WordPress applications, but even custom plugins can require dependencies not available to WordPress out of the box. As such, many developers have turned to Composer - a dependency manager for PHP applications.

Composer has many legitimate use cases, but proceed with caution. Whenever you introduce dependencies like this you are implementing an unmanaged variable to a managed hosting environment. Should the dependency cease to exist, or composer's autoloader fails to implement the dependency as intended, Pagely will have no way to track this. Having said that, Pagely has provided a convenient way for you to utilize Composer, should all other methods available to you be exhausted.

Some tips when utilizing Composer...

  1. Narrow your Scope - does composer really need to be installed at the Application level? Can it be narrowed to the plugins directory, or even a plugin itself? The more narrow the scope, the less likely your entire site will fail in the event the dependency fails you.
  2. Avoid things Pagely already does for you - WordPress Packagist is great, but Pagely already manages and updates your plugins for you that resides on wordpress.org. Unless its a custom plugin, you won't have much of a need for this.

How do we get started with Composer?

Composer comes pre-installed on Pagely VPS plans, so we can skip that step and jump right to installing a package. Let's assume we want to install querypath at the application level in this example.

QueryPath is a PHP library for manipulating XML and HTML. It is designed to work not only with local files, but also with web services and database resources. 

First, lets cd into your root directory...

pagelyUser@pagelyvps.com$ls ~/sites
composer-test.pagelyexample.com
pagelyUser@pagelyvps.com$cd ~/sites/composer-test.pagelyexample.com
pagelyUser@pagelyvps.com$ls
wp-admin wp-cron.php wp-settings.php
wp-blog-header.php wp-includes wp-signup.php
db-config.php wp-comments-post.php wp-links-opml.php wp-trackback.php
index.php wp-config-hosting.php wp-load.php xmlrpc.php wp-config.php wp-login.php
wp-activate.php wp-content wp-mail.php

We can simply add this as a requirement...

composer require {DEPENDENCY}

The require command adds new packages to the composer.json file from the current directory. If no file exists one will be created on the fly.

pagelyUser@pagelyvps.com$ composer require querypath/querypath
Using version ^3.0 for querypath/querypath
./composer.json has been created
Updating dependencies (including require-dev)
- Installing masterminds/html5 (2.4.0)
Downloading: 100%

- Installing querypath/querypath (3.0.5)
Downloading: 100%

Writing lock file
Generating autoload files

After this, you'll notice one new directory, and two new files in your current directory...

  1. composer.json
  2. composer.lock
  3. vendor
$cat composer.json
{
"require": {
"querypath/querypath": "^3.0"
}
}
pagelyUser@pagelyvps.com$ls vendor/
autoload.php composer masterminds querypath
pagelyUser@pagelyvps.com$ls vendor/querypath/querypath/
API CREDITS patches src
bin examples pear-summary.txt test
build.xml INSTALL phar tutorials
composer.json Makefile quickstart-guide.md
config.doxy package_compatible.xml README.md
COPYING-MIT.txt package.xml RELEASE

Okay, now we need to ensure the vendor/autoload.php above is utilized with our application. Since we want this to be accessible ACROSS our WordPress application, we can include our autoloader dependency file within wp-config.php

pagelyUser@pagelyvps.com$head -3 wp-config.php
<?php
// if require_once is too strict, try require, but it should work
require_once __DIR__.'/vendor/autoload.php';

If we had a more narrow scope, say for a theme, we might include this line within our theme's functions.php.

At this point you should be able to utilize the dependency into your stack!

Related Topics

Pagely is the Managed WordPress Hosting Platform designed to exceed the needs of media, business, and Enterprise customers alike. We help the world's biggest brands scale WordPress.

Copyright © 2006-2017 Pagely, Inc. All rights reserved.
Pagely® and WordPress® are registered trademarks.

Pagely
Powered by Zendesk