Wordpress development workflow using composer

Motivation

I'm a Drupal developer who have been using composer since a lot of time ago. I wanted to take a look on how to setup a Wordpress project using composer to take advantage of all the goodness it brings to the projects.

If you have previously worked with composer, you already know that it allows you to manage your dependencies using simple commands. This way, you add only the necessary code to your repo and not tons of contributed that you can pull only when you need.

How to do it?

There's no official out of the box solution to manage Wordpress with composer. That's when johnpbloch packages come to the rescue. It allows you to install wordpress using composer by simply requiring it as a composer package:

composer require johnpbloch/wordpress:^5.4

It's recommended to set prefer-stable to true before requiring packages so that it avoids dev packages when possible.

There's also a packagist repo for the plugins and themes that you can find at https://wpackagist.org/. You should setup the repo in your composer.json as following:

"repositories": [

        {

            "type": "composer",

            "url": "https://wpackagist.org"

        }

],

And require any package as following:

composer require wpackagist-plugin/woocommerce

Doing this you'll get the plugins and themes in your project root folder; you'll need to find a way to move them to the right location. There's a package on packagist that it's supposed to be used for this: fancyguy/webroot-installer; however, I wasn't able to make it work so I used my handy composer-symlinks package with the following configuration:

"extra": {

        "symlinks": {

            "wp-content/plugins": "wordpress/wp-content/plugins",

            "wp-content/themes": "wordpress/wp-content/themes"

        }

}

The final composer.json file can be found in the following link: https://github.com/kporras07/wpcommerce/blob/master/composer.json 

I even added my local development environment project Chirripo to this project so that everything is contained in the same repo (Disclaimer: stock Chirripo images don't have mysqli or soap extensions so they need to be installed as explained in README.md).

Conclusion

Thanks to some efforts of the great Wordpress community it's possible to manage dependencies using composer and it works smoothly so, it's really worth to give it a try and make it work for your next Wordpress project.

Submitted by kporras07 on