Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Managing a WordPress Site as a Composer Project by Rahul Bansal @ WordCamp Nagpur 2017

998 views

Published on

This presentation explains the advantages of using Composer dependency manager to manage complete WordPress projects, including themes, plugins and WordPress core itself.

This method makes developing, maintaining & scaling WordPress sites in a long run easy.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Managing a WordPress Site as a Composer Project by Rahul Bansal @ WordCamp Nagpur 2017

  1. 1. Managing a WordPress Site as a Composer Project @rahul286 #WCNAGPUR
  2. 2. Managing a WordPress Site
  3. 3. Involves… Source Uploads  Database Config
  4. 4. Involves  core themes plugins wp-content ↪︎ uploads database wp-config.php
  5. 5. Operations Changes Updates Multi Server
  6. 6. Manage Code via Git
  7. 7. ƶ
  8. 8. 1 site = 1 repo🤘
  9. 9. • Makes it easy to control code changes • Collaborative workflow w/git branching • Easy to deploy same codebase in autoscaling/ multi-server setup Benefits
  10. 10. • Put everything in a git repo • Disable code changes on live • Disable direct code upload • Force every code change go through git How?
  11. 11. • Git repo grows big and fast in size • It takes longer to clone entire repo • git clone always downloads complete repo • Some plugins are required for development/ debugging only Issues
  12. 12. Composer
  13. 13. Dependency Manager
  14. 14. Hello Composer!
  15. 15. Composer Concepts ǽ composer.json Ɨ Package Ƙ Repository
  16. 16. • WordPress core, themes and plugins can be declared as composer dependency • Reduces repo size as actual core/theme/ plugin codes are not inside git repo Benefits in WordPress
  17. 17. • Separation of dev dependency such as query- monitor, debug-bar plugins • Non-WordPress packages can be mixed such as php-code-sniffer, codeception, wp-cli Benefits in WordPress
  18. 18. Demo 🍿
  19. 19. Install Composer
  20. 20. curl -sS https://getcomposer.org/installer | php -- --filename=composer —install-dir=/usr/local/bin Install Composer on Linux
  21. 21.  brew install composer Install Composer on Mac
  22. 22. Start a new composer project
  23. 23. git init Step #0
  24. 24. composer init or composer create-project … Install Composer on Mac
  25. 25. Install WordPress Core
  26. 26. composer require johnpbloch/wordpress or { "require": { "johnpbloch/wordpress": "4.2.2" } } Add WordPress Core
  27. 27. Create a Mysql Database mysql -uroot -e "create database wpcomposer" Config file cd wordpress wp config create --dbname=wpcomposer --dbuser=root Complete Setup
  28. 28. •Move wp-config.php outside WordPress root •Copy index.php and edit last line to correct path to wp-blog-header.php Giving WordPress it’s own directory
  29. 29. //public location define('WP_HOME','http://example.com'); //wordpress location define('WP_SITEURL','http://example.com/wordpress'); //wp-content location change define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/wp-content' ); define( 'WP_CONTENT_URL', 'http://example.com/wp-content' ); Tweaking URLs and Paths
  30. 30. //disable auto updates define( 'AUTOMATIC_UPDATER_DISABLED', true ); //disable theme/plugin file editing UI define( 'DISALLOW_FILE_EDIT', true ); define( 'DISALLOW_FILE_MODS', true ); Disable updates
  31. 31. • Avoid putting sensitive data in wp-config.php • Use .env file instead • Optionally, with - https://github.com/scottjs/wp-dotenv composer require “scottjs/wp-dotenv:1.*" .env file
  32. 32. Add WordPress.org Theme & Plugins
  33. 33. composer config repositories.0 composer https:// wpackagist.org/ or "repositories": [{ "type": "composer", "url": "https://wpackagist.org/" }] wpackagist composer repo
  34. 34. composer require wpackagist-theme/p2 or "require": { … "wpackagist-theme/p2": “^1.5" … }, Add a wordpress.org theme
  35. 35. composer require wpackagist-plugin/wordpress-seo or "require": { … "wpackagist-plugin/wordpress-seo": "^4.9", … }, Add a wordpress.org plugin
  36. 36. composer require --dev wpackagist-plugin/debug-bar or “require-dev": { … “wpackagist-plugin/debug-bar”: “^0.9.0", … }, Add a DEV-only wordpress.org plugin
  37. 37. Add non-WordPress.org Theme & Plugins
  38. 38. { "name": "org-name/project-name", "type": "wordpress-plugin", "require": { "composer/installers": "^1.0" } } Add composer support in own plugins
  39. 39. "repositories": [ { "type": "vcs", "url": "git@github.com:org-name/project-name.git" } ], "require": { “org-name/project-name":"^1.0" }, VCS + Composer
  40. 40. { "type": "package", "package": { "name": "org-name/package-name", "type": "wordpress-plugin", "version": "dev-master", "source": { "type": "git", "url": “git@github.com:org-name/package-name.git", "reference": "master" } } } VCS only. NO Composer
  41. 41. { "type": "package", "package": { "name": "org-name/package-name", "type": "wordpress-plugin", "version": "1.0.0", "dist": { "type": "zip", "url": "https://example.com/downloads/package-name.zip" } } } Zip only. NO VCS. NO Composer
  42. 42. Too many cases? 🤔
  43. 43. Present on a composer repo Using VCS Has composer.json Define Repository Example packagist.org Implied Implied Not Required Any composer package, including some WordPress core/theme/plugin wpackagist.org Implied Implied Add wpackagist.org composer repo WordPress.org themes/plugins Other composer repo Implied Implied Add other composer repo Specific packages provided from those repo 𐄂 ✓ ✓ Type => VCS A package hosted on VCS with composer.json but not published on any composer repo e.g. your forks 𐄂 ✓ 𐄂 Type => Package Source => VCS A package hosted on VCS repo only e.g. a plugin hosted on GitHub only 𐄂 𐄂 𐄂 Type => Package Dist => zip Usually a premium theme/plugin available as a zip file from special URL
  44. 44. Updating WordPress Core, Theme & Plugins
  45. 45. 1. Edit composer.json if required 2. Run composer update locally 3. Test Changes 4. Verify composer.lock 5. Commit composer.lock 6. Run composer install production server Ideal Workflow
  46. 46. composer.json vs composer.lock
  47. 47. composer.json composer.lock Human Editable Yes 👶 Avoid ⛔ File Changes Mostly direct edit or composer require Mostly composer require/update Environment Development Production Download Deps composer update composer install Speed Slow Fast
  48. 48. Moving Existing Site Management to Composer
  49. 49. Jai WordPress! 🙏 @rahul286 rahul@rtcamp.com

×