WordPress and Composer TLDR

There’s a few questions I’m commonly asked about using composer + WordPress. Here they are in a super short form FAQ:

To install a composer project, run:

composer install

To update a composer project, run:

composer update

Install WordPress in a folder

composer require johnpbloch/wordpress @stable

or in your composer.json add:

{
    "require" : {
        "johnpbloch/wordpress": "@stable"
    }
}

Note: This is a subdirectory install, you really don’t want to nest composer packages. If you want plugins/themes etc under wp-content, I recommend using WP CLI instead to install and update WordPress Core

Install a plugin in a WordPress Install

composer require plugin-package-name-goes-here *

or in your composer.json add:

{
    "require" : {
        "plugin-package-name-goes-here": "*"
    }
}

Install a Theme in a WordPress Install

composer require theme-package-name-goes-here *

or in your composer.json add:

{
    "require" : {
        "theme-package-name-goes-here": "*"
    }
}

Manage only Themes/Plugins with Composer

It’s easy, just don’t mention WordPress core in your composer.json file

You don’t use Composer but you’d like to

Install Composerpress, and under the tools menu will be a new settings page. Visit this page and Composerpress will scan your WordPress install and reverse engineer a composer.json for you. Copy and paste it into a composer.json file, and you’re good to go

You want to add Composer support to a theme

Put a composer.json at the root of your theme with this in it:

{
    "name" : "package/name",
    "description": "Your themes description",
    "keywords" : ["wordpress"],
    "type" : "wordpress-theme",
    "license" : "GPL-3.0",
    "require" : {
        "composer/installers" : "~1.0",
    }
}

 

You want to add Composer support to a plugin

Put a composer.json at the root of your plugin with this in it:

{
    "name" : "package/name",
    "description": "Your plugins description",
    "keywords" : ["wordpress"],
    "type" : "wordpress-plugin",
    "license" : "GPL-3.0",
    "require" : {
        "composer/installers" : "~1.0",
    }
}

Want to use a theme from WordPress.org

{
    "repositories":[
        {
            "type":"composer",
            "url":"http://wpackagist.org"
        }
    ],
    "require" : {
        "wpackagist-theme/theme-name": "*"
    }
}

You Want to use a plugin from WordPress.org

{
    "repositories":[
        {
            "type":"composer",
            "url":"http://wpackagist.org"
        }
    ],
    "require" : {
        "wpackagist-plugin/plugin-name": "*"
    }
}

Declare that the plugin depends on another plugin on WordPress.org

Inside the plugins composer.json:

{
    "repositories":[
        {
            "type":"composer",
            "url":"http://wpackagist.org"
        }
    ],
    "require" : {
        "wpackagist-plugin/other-plugins-name: "*"
    }
}

Declare a minimum PHP version

{
    "require" : {
        "php": ">=5.4"
    }
}

Use Composer inside a Single Theme or Plugin

Option 1: Install the theme/plugin and run composer install in that folder

Option 2: Create a folder for the plugin and inside that folder run composer require theme-package-name *

Should I put my Theme/Plugin on Packagist?

If you must, but, Packagist is for things that are of interest to the broad PHP community, not a sub-community such as WordPress. Use WPackagist and the WordPress.org system instead.

6 thoughts on “WordPress and Composer TLDR

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.