Simon Pollard
Web developer
Using Composer with WordPress
@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
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
You need to install composer on your machine...
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');"
@smp303
Bit trickier... best to follow their guide:
@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
@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
"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
/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
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
@smp303
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
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
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
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
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
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
@smp303
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
@smp303
These slides and more from me...
https://slides.com/simonp303
Composer: https://getcomposer.org
WPackagist: https://wpackagist.org
Roots Guide: https://roots.io/using-composer-with-wordpress/
By Simon Pollard
Using composer with WordPress