Not long time ago, we introduced Composer support for the premium extensions of Meta Box. However, in the actual project, sometimes you need free Meta Box extensions hosted on WordPress.org. Extensions such as MB Rest API and MB Relationships are really useful for some of your projects. Then how to use Composer for the WordPress plugins on wordpress.org? This article will show you how to do that.
Introduction to Composer
Composer is a dependency management package for PHP. It is similar to NPM for NodeJS. When you work on an X project, you need to use a number of libraries A, B and the C, D plugins to complete the tasks. Thus, X will depend on A, B, C and D.
As a general rule, you will have to manually install A, B, C, D through the plugin manager of WordPress or include them in X’s plugin. However, by using Composer, you only need to declare libraries in a file named composer.json and running acomposer install command. All libraries will be installed automatically for you.
You can see more about the Composer at its own homepage.
Benefits of Composer
There are two basic benefits of the Composer for developers:
- Manage and install dependencies conveniently and quickly
- Bundles are required by the components in a single package, and thus hide their visibility to end users.
The second benefit is quite useful when you do not want your customers to see a list of about 20 plugins installed in the admin area in WordPress. Although the number of plugins not as important as their quality, hiding the list of plugins can help end users feel more comfortable.
In the case of Meta Box, as more and more extensions are available (both premium and free), there is a growing need to bundle extensions into a single package. And that works for premium extensions. The rest are free extensions.
Installing free Meta Box extensions with Composer
The free extensions of Meta Box are also available on packagist.org. So, you can install them directly just like other Composer packages.
Suppose you have a plugin X and you want to bundle the free extensions MB Rest API and MB Relationships of the Meta Box to the vendor/meta-box subdirectory, you simply declare the following in the composer.org file:
{
"require": {
"wpmetabox/meta-box": "^5.12",
"wpmetabox/mb-rest-api": "^2.0",
"wpmetabox/mb-relationships": "^1.12"
},
"autoload": {
"files": [
"vendor/wpmetabox/meta-box/meta-box.php",
"vendor/wpmetabox/mb-rest-api/mb-rest-api.php",
"vendor/wpmetabox/mb-relationships/mb-relationships.php"
]
}
}
Then just run the composer install command, you will see that the plugin is downloaded and placed in the vendor/wpmetabox directory of your plugin.
To use these plugins, simply add the following to your PHP plugin file:
require 'vendor/autoload.php';
Note that, when plugins are plugged into your plugin, there is no place to activate them. So you have to load these plugins through the autoload mechanism of the Composer, and when load the Composer's autoload.php file, all the plugins will be loaded.
composer.json file, which contains a "full" list of extensions, free and premium. You can grab it here and remove the extensions you don't want to use.Video demo
This is the demo video that I record all the steps above:
Conclusion
Using Composer has a lot of advantages, it just helps you manage the plugin you need, just to help you hide them in the plugin list of WordPress. And with the Composer support for all extensions, free and premium, now you can bundle any of the extensions you want in your product. What do you think about this? Please let me know in comments.
Meta Box AIO Now Includes Free Extensions
Meta Box Weekly Updates: Facebook Group, Admin Menu, Composer Support
Introducing Composer Support for Meta Box Extensions