Setting up WordPress with Composer - Or: how to teach a budgie to talk
Who does not know this situation: There is once again a WordPress (security) update and magically after the update, the Akismet plugin, as well as two not at all needed or used themes were installed.
Since by default WordPress comes only as a complete package with just this plugin and at least the current theme, the subsequent deactivation / uninstallation of the unneeded aspects was just the bullet we have bitten. Furthermore, for our WordPress updates many manual steps were still necessary, but we have gradually automated more and more to reduce the complexity of the supposedly easy task "perform WordPress update".
Now, as an established TYPO3 agency, we have been consistently using Composer at least since the introduction of TYPO3 version 8.7 LTS and have since enjoyed the advantages. So why not also try to set up WordPress with Composer? We thought so too. No sooner said than done.
First of all: Yes, it works and even more than satisfactorily.
How exactly we proceeded, you will now learn in the following post. If you prefer to start with a "basic" configuration, you can have a look at the repository here. There I have set up an example repository with which you can start directly.
Step 1: set up composer.json initally
Let's go - Step 1: Back to the beginning and we rebuild our blog again technically from scratch with Composer. The biggest hurdle was to pull the WordPress core only selectively with certain components. We got inspired by this page here (https://roots.io/bedrock/) and quickly found out that this is exactly what we are looking for. In the composer.json you must then pay attention to the following information regarding the storage locations:
- Cores (wordpress-install-dir)
- Plugins (type:wordpress-plugin)
- Custom Themes (type:wordpress-theme)
- Languages (vendor:koodimonni-language)
Because we wanted to additionally set up the configuration of our blog depending on the environment, we controlled this via the application.php file. This gives us the possibility for each environment to pull the access data for the database and the root path of the WordPress instance. Please note that you need to adjust the root path here accordingly, depending on which path you choose in the composer.json (wordpress-install-dir). Now create a customized wp-config.php and you are ready to go. (A .htaccess file should not be forgotten by the way 😉)
Step 2: Use wpcli for plugins
The translations of the core can then also be integrated with the koodimonni-language repository using Composer. Thus, we now have the complete core - and right in our desired target language. What is still missing is a language update of the plugins. This is where wpcli comes into play. With the help of "wpcli/language-command" and "wpcli/extension-command" we can now execute the following commands (replace _WPCLI_ with your path to wpcli):
This command activates all installed plugins:
_WPCLI_ plugin activate --all --path=_YOUR_WEB_PATH_ --debug=boostrap
Installs all language files for all active plugins for the language de_DE ("_YOUR_WEB_PATH_" should be replaced by the path to the installation):
_WPCLI_ language plugin install de_DE --all --path=_YOUR_WEB_PATH_
Updates all language files for all plugins:
_WPCLI_ language plugin update --all --path=_YOUR_WEB_PATH_
Step 3: Integrate a custom (child) theme
A custom (child) theme can now be added in step 3 via composer.json. If you only want to adapt a standard theme, you should set a requirement for the standard theme in your child theme in composer.json. Again, do not forget the package for the translation of the theme as a dependency 😉. Now you can start!
Et voilá: You have set up your WordPress with Composer!
I hope that everything was understandable so far. We are always happy to receive praise, criticism or suggestions as well as experiences/reports on how you got along with our instructions.
By the way, I already gave a presentation on this topic at WordCamp Cologne 2018, which you can watch here. I have included updates in the article here. You can find the example repository at: https://github.com/sfsmfc/wp_composer_starter. And now go ahead and good luck with your own WordPress project.
Please feel free to share this article.
Comments
No comments yet.