A GUIDE TO USE COMPOSER WITH WORDPRESSApril 8, 2019
A dependency manager for PHP, composer is in use from the last few years for advanced PHP development. A composer repository, Packagist is a place where installable packages are present for Composer.
Today, we at Wetech Digital will cover the use of Composer and how to initialize the integration of Composer with WordPress site through this detailed post.
Why Use a Composer with WordPress?
Reusing code is a good thing as you will not get extra pay for revolving the wheel, instead, they will pay in turn of a high-quality application or website. So, you already have a batch of go to plugins that can be employed for all the WordPress websites you develop. However, via plugins, you are already using the code of others, but a wide environment of advanced PHP libraries is available for you via composer.
To make the plugin live, as a composer dependency, someone is working themselves for that.
A part of your build pipeline can be a dependency manager which can keep out the third-party code from the source repository. It makes the path clear for utilizing the PHP libraries unavailable otherwise.
If you are among those developers who just hack the plugins to throw the action they wish then a dependency manager will make you think in a different manner about how to accomplish the same in another way.
Starting with Composer
As you are already a part of this, you are aware of the way to move on by making WordPress a part of a subdirectory. You might not be strange to a submodule named, Mark Jaquith suggest. That’s good! It shows us the origin mark of composer: let us transform the WordPress submodule to a composer dependency.
Firstly, the submodule is needed to be removed. This will not be included here.
At the base of your project, Composer’s role is to install libraries into vendor directory. Based on the package type, you are given the authority by the installer to make changes in the behaviour. We need to install WordPress on the document root in the custom location as it is not a library. Since it is already available, so there is no need to make it ourselves.
Installation of WordPress
At the root of the project, with a “composer.json” file, composer dependencies are defined. This file is included in the auto-updated fork of WordPress and is too on packagist.
The WordPress itself and WordPress installer is included in the most simple rustic composer file. You can then notify the WordPress installer through an extra section about where to put WordPress.
Installation of Plugins and Custom Repositories
After having installed WordPress, some plugins need to be installed with composer. Do you remember installer (a bit above)? For wordPress themes and plugins, there exist custom installer which is a part of the installer/composer library. So, just pull that in and guide composer where to post the things.
From WordPress packagist, as a composer dependencies, to install plugins we will too need to append it in our composer.json file as a repository.
To deploy the WordPress website correctly, be sure that your composer files and vendor folder are inaccessible to the public and absent from your document root. If your first step takes the help of a composer, that might not be done, so it’s better to block those files with the server configuration.
Go for the First Step
After using composer singly, it would be impossible to step back. You should go for it, you may try installing WordPress through composer and move further from there.