Johann Savalle

@yasha.solutions

Product designer and full stack developper living in france
< Back to all articles

WordPress with Composer

August 14, 2019

This is the killer workflow for WordPress, or at least the key element to build one.

Composer

Composer is a great tool to manage your PHP dependencies. You probably already know about it if you are used to working with PHP.

composer

Composer is to PHP what npm is to javascript or pip is to Python.

If you come from WordPress integration world, then it might be new to you, but if you want to systematise your install and updates process, this is a great improvement from the traditional WordPress way.

shell > composer install
shell > composer update

Key Concepts of Composer

Where we talk about composer file (composer.json), Packagist

If you know composer you can basically skip that part

Composer.json

  • The central file that control all the dependencies.
  • Very similar to package.json in a nodeJS app.
  • Structure
{
    "require": {
        "vendor/package": "1.3.2",
        "vendor/package2": "1.*",
        "vendor/package3": "^2.0.3"
    }
}

Packagist: where the packages lives

  • Packagist.org is probably the greates source of public packages (like npm.org in JS)

Packagist

Packagist hosts roughly over 200K PHP packages

If you need a PHP package - it is likely to be on Packagist

Composer basics : install and elementary commands

installation is a one liner : curl -sS https://getcomposer.org/installer | php

Common commands

$ composer require vendor-name/package-name
$ composer install
$ composer update

In short

Now you know:

  • Composer is a tool to manage PHP packages
  • How to install composer and basic commands
  • Packagist is where are most of PHP packages lives

Composer and WordPress

Where we introduce WPackagist, and we modify WordPress folder structure for a cleaner app logic.

WordPress and Composer

WPackagist

Basically Packagist for wordpress themes and plugins This is where you will source most of the plugin installs

WPackagist

Typical WordPress Composer Json config file

(TODO: replace this shameless copy paste from WPackagist with a better example)

{
    "name": "acme/brilliant-wordpress-site",
    "description": "My brilliant WordPress site",
    "repositories":[
        {
            "type":"composer",
            "url":"https://wpackagist.org"
        }
    ],
    "require": {
        "aws/aws-sdk-php":"*",
        "wpackagist-plugin/akismet":"dev-trunk",
        "wpackagist-plugin/wordpress-seo":">=7.0.2",
        "wpackagist-theme/hueman":"*"
    },
    "autoload": {
        "psr-0": {
            "Acme": "src/"
        }
    }
}

Composer and WordPress - real world example

In a typical scenario you need:

  • Wordpress itself
  • Plugins most of them public but maybe a few premium
  • A Theme

WordPress itself

You get it from John P. Blosh

And yes, call WordPress from composer make wordpress a dependency.

I know it might feel weird at first, but it's ok, once you make wordPress a simple dependency of of the project and not the core of the app it makes things easier.

Concretly, it means :

  • wp-content is where most of the valuable data live - that's the core of your app and has a life of his own - independently of the main WordPress dependency - it makes sense therefore to separate wp-content from the rest.
  • wordpress itself can be installed in its own sub-directory
wp-content
index.php
wp
wp-config.php

Plugins

You get it from

  • wpackagist and git repos

Theme

  • You get it from a git repo

Git all the things

Where we discuss version control the things

< Back to all articles