Not long time ago, I 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.

How to use Composer to install free Meta Box extensions

The free extensions of Meta Box are hosted on wordpress.org. By default, Composer does not work with packages there. It only works with a repository of packages at packagist.org only.

However, thanks to WordPress developers who created a free repository for the plugin on wordpress.org at wpackagist.org. Now you can use it in Composer to install the plugins on wordpress.org.

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:

{
  "repositories":[
    {
      "type": "composer",
      "url": "https://wpackagist.org"
    }
  ],
  "require": {
    "wpackagist-plugin/meta-box": "dev-trunk",
    "wpackagist-plugin/mb-rest-api": "dev-trunk",
    "wpackagist-plugin/mb-relationships": "dev-trunk"
  },
  "extra": {
    "installer-paths": {
      "vendor/meta-box/meta-box/": ["wpackagist-plugin/meta-box"],
      "vendor/meta-box/mb-rest-api/": ["wpackagist-plugin/mb-rest-api"],
      "vendor/meta-box/mb-relationships/": ["wpackagist-plugin/mb-relationships"]
    }
  },
  "autoload": {
    "files": [
      "vendor/meta-box/meta-box/meta-box.php",
      "vendor/meta-box/mb-rest-api/mb-rest-api.php",
      "vendor/meta-box/mb-relationships/mb-relationships.php"
    ]
  }
}

Then just run the composer install command, you will see that the plugin is downloaded and placed in themeta-box directory of your plugin.

install plugins via composer

To use these plugins, simply add the following to your PHP plugin file:

require 'vendor/autoload.php';

And now in the plugin section of WordPress there is only one plugin for your X. Other plugins, including Meta Box, MB Rest API and MB Relationships are hidden.

To make it easier for developers, I’ve created a 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.

Some important notes

You can install the Meta Box directly from wpackagist.org

Because the Meta Box plugin is hosted on WordPress.org, you can install it from wpackagist.org just like other plugins. In the code above, I added the Meta Box to the list of plugins to install.

"wpackagist-plugin/meta-box": "dev-trunk"

Alternatively, you can use a separated package on packagist.org. See details here.

Change the path to the directory where the extensions are installed

By default, plugins installed from wpackagist.org will be placed in the wp-content/plugins directory. This is because the wpackagist uses a small utility called Composer Installers. This utility allows you to set paths for the installed packages. By default, for packages of the type wordpress-plugin, the default path is wp-content/plugins.

If you use Composer to manage your entire website, this is reasonable. However, in case you want to bunlde the plugin into your plugin (as I did above), then you need to revise the path, as I did:

"extra": {
  "installer-paths": {
    "vendor/meta-box/meta-box/": ["wpackagist-plugin/meta-box"],
    "vendor/meta-box/mb-rest-api/": ["wpackagist-plugin/mb-rest-api"],
    "vendor/meta-box/mb-relationships/": ["wpackagist-plugin/mb-relationships"]
  }
},

Autoload the plugin files

As mentioned above, plugins are installed by default in the wp-content/plugins/ directory and therefore, you have to access the All Plugins screen in WordPress and activate them.

However, 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 thanks to the following:

"autoload": {
  "files": [
    "vendor/meta-box/meta-box/meta-box.php",
    "vendor/meta-box/mb-rest-api/mb-rest-api.php",
    "vendor/meta-box/mb-relationships/mb-relationships.php"
  ]
}

The autoload is quite simple, you just load the main PHP files of the plugins.

Removing the .svn folders

By default, if you run composer install, it will automatically download the source folder from wordpress.org. And those source folders contains the .svn folder, which is used for control version system (SVN) on wordpress.org. The .svn folder just contains the versions info and it’s useless for us. So it’s better to remove them.

To install the plugins without .svn folders, please use this command:

composer install --prefer-dist

And now you have clean folders.

Updates

One thing to note that using Composer means that you use a different “plugin manager”. Composer will manage all the plugins instead of WordPress. So WordPress doesn’t check plugins for updates. And when you want to update, you have to do that manually via the command:

composer update

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. With the above method, you can apply not only to the free extensions of Meta Box but also to all other plugins on wordpress.org.

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.


Also published on Medium.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.