Composing WordPress

Using Composer with WordPress

Simon Pollard

Full Time Web Developer at 

 

 

 

@smp303

 

https://slides.com/simonp303

What is composer?

@smp303

Composer is a tool for dependency management in PHP.


It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Ehh????

@smp303

@smp303

What is composer?

Composer is like a shopping list.

 

It allows you to create a list of things you need and fetches them for you.

 

A bit like Tesco Delivery...
Only it works...
And you get what you asked for!

 

@smp303

So how do I use it?

You need to install composer on your machine...

https://getcomposer.org/

 

On a Mac via terminal (don't worry just copy and paste)

php -r "readfile('https://getcomposer.org/installer');" > composer-setup.php
php -r "if (hash('SHA384', file_get_contents('composer-setup.php')) === '7228c001f88bee97506740ef0888240bd8a760b046ee16db8f4095c0d8d525f2367663f22a46b48d072c816e7fe19959') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

On Windows..

@smp303

Bit trickier... best to follow their guide:

Creating your list

@smp303

Create composer.json in the root of your project

{
  "name": "companyname/myproject",
    "authors": [
        {
            "name": "Simon Pollard",
            "email": "simon.pollard@shiftactivemedia.com"
        }
    ]
}

Define the project you are working on and add in the authors

Get WordPress

@smp303

"repositories": [
    {
      "type": "package",
      "package": {
        "name": "wordpress",
        "type": "webroot",
        "version": "4.4.2",
        "dist": {
          "type": "zip",
          "url": "http://wordpress.org/wordpress-4.4.2.zip"
        },
        "require" : {
          "fancyguy/webroot-installer": "1.0.0"
        }
      }
    }
]

Get it direct from WordPress

@smp303

Get WordPress

  "require": {
    "php": ">=5.3.0",
    "wordpress": "4.4.2"
  },
  "extra": {
    "webroot-dir": "public/wp",
    "webroot-package": "wordpress",
  }

"Require" it and add it to the right folder

We have PHP as a dependency - to make sure things will run.

 

We then install WordPress in the specified folder

@smp303

Get WordPress

/public/wp ??

 

For me (you can put it where you like) but...

 

/wp                     < just wordpress (dont ever touch)

/wp-content     < all your themes/plugins here

.htaccess           

index.php

wp-config.php     < a few alterations needed (ask me)

@smp303

Running Composer

Install for the first run...

 

composer install
php composer.phar install

If you have composer installed on your machine

Note: you may need to "sudo" the command

sudo composer install

You can download a .phar file (like a program) and use that to run your command...

Very handy on a server!

@smp303

Installed!

@smp303

Next steps...

You now have a composer.lock file, this is basically saving your config as it stands.

 

You can run composer again now with...

composer update

This will only get/update what you asked for and what is in the lock file. 

 

If you update your composer.json file, run composer install again.

@smp303

Plugins...

If you are lucky...

Some lovely person created all the plugins as composer packages here:
https://wpackagist.org/

 

You will find all public plugins here and the code needed to add them to your composer.json file...

@smp303

Plugins...

Adding to composer.json

"repositories": [
    {
      "type": "composer",
      "url": "http://wpackagist.org"
    }
]
"require": {
    "wpackagist-plugin/wordpress-seo": "2.3.5",
    "wpackagist-plugin/w3-total-cache": "0.9.4.1"
  }
  "extra": {
    "installer-paths": {
      "public/wp-content/plugins/{$name}/": ["type:wordpress-plugin"]
    }
  }

@smp303

Premium Plugins?

Use Git

"repositories": [
    {
      "type": "package",
      "package": {
          "name": "companyname/gravityforms",
          "type": "wordpress-plugin",
          "version": "1.9.15",
          "source": {
            "url": "git@bitbucket.companyname/gravityforms.git",
            "type": "git",
            "reference": "1.9.15"
          }
      },
      "require": {
        "composer/installers": "~1.0"
      }
    }
]
  "require": {
    "companyname/gravityforms" : "1.9.15"
  },

@smp303

Themes?

Also on wpackagist

  "extra": {
    "installer-paths": {
      "public/wp-content/themes/{$name}/": ["type:wordpress-theme"]
    }
  }
"require": {
    "wpackagist-theme/shop": "1.02"
  }

Make sure they go in the right place

@smp303

Custom Themes?

You could do the same with plugins and use Git to pull them in.

 

Or... stick around and I will show you a better way to do things :)

@smp303

Why is it any good?

  • Very easy to set up a site, anywhere
  • All team members get the same set up
  • Very easy to update packages
  • No need to version control loads of code,
    especially WordPress in all your projects
  • Version 1.0.0 is out today! 5 years in the making
  • And...

@smp303

Bonus...

 

Use it with Git... save the composer files into your repo, then after you checkout your repo run composer.

 

Full website without storing the whole lot in Git!

 

Extra bonus - Capistrano can run composer with deployment! This is amazingly uselful

 

Ask me for a demo

Resources

@smp303

These slides and more from me...
https://slides.com/simonp303

 

Composer: https://getcomposer.org

WPackagisthttps://wpackagist.org

 

Roots Guidehttps://roots.io/using-composer-with-wordpress/

Composing WordPress

By Simon Pollard

Composing WordPress

Using composer with WordPress

  • 1,630