Using Composer to Manage Own Theme/Plugin Dependencies (for developers)

As you may already know, WordPress doesn’t support composer so our themes/plugins distributed as zip files won’t benefit from any composer magic.

But we can surely use composer in our development environment to manage development dependency and with some extra steps also manage production dependencies.

Using composer.json to manage development dependency

Following is part of snippet from example presented in last chapter:

    "require-dev": {
        "wpreadme2markdown/wpreadme2markdown": "*"
    }

In above example, we have declared development dependency for WP-Readme-to-Github-Markdown project.

This is a simple script we run to convert readme.txt file to readme.md file i.e. from WordPress style readme to Github style readme. As Github readme is not required for WordPress projects, we don’t need this script in wordpress.org zip distribution.

Composer by default installs dev dependency. So when you have require-dev section defined, in production, pass --no-dev flag to composer install/update commands.

ADVERTISEMENT

You can define any other packages e.g. libraries, additional tools/scripts needed during development environment.

Using composer.json to manage production dependency

Even though WordPress doesn’t support composer, you can also manage production dependencies using composer.

Let’s have a look at an example from famous WordPress-SEO plugin’s composer.json file:

"require": {
    "composer/installers": "~1.0",
    "yoast/license-manager": "^1.2",
    "yoast/i18n-module": "^1.0",
    "xrstf/composer-php52": "^1.0.17"
},

As you can see wordpress-seo plugin declares some production dependencies. As these are standard composer packages, they will go into vendor directory.  On github repo, you will see vendor directory is missing. But it’s present on SVN repo.

Build Stage/Script

If you have used grunt/bower/SASS like tools, you may be aware of a build stage where we do some extra processing before we can release code for production usage.

When managing dependencies via composer, we need to add a build stage where before exporting git repo as a zip module, we will run composer install. You can of courser, automate your build process using a script. Currently we do not have a standard build script, but we hope to publish one soon.

You may subscribe to our newsletter for updates.