Delicious Brains

Documentation

Installing via Composer

14002042262_f43b4f2e9b_o

The Conductor – Inspired by Amadeus by Canary Beck. CC BY-NC-SA 2.0

We occasionally get requests for help in installing our WP Migrate DB Pro plugin into WordPress via Composer. Here’s a short walkthrough to get you started.

Assumptions

  • You’ve got Composer installed and working.
  • You have a WordPress site up and running.
  • You do not currently have a composer.json file for your site yet, or are savvy enough to pick apart the following walkthrough to update your composer.json file.
  • You do not currently have WP Migrate DB Pro or its addons installed on the WordPress site.

Plugins - No WP Migrate DB Pro Installed

composer init

First create a composer.json file in the root of your WordPress site.

composer init

[“type:wordpress-plugin”]

Composer does not know what a WordPress plugin is, or what to do with them, so add an “extra” section to your composer.json file that rectifies that. And while you’re at it, why not tell Composer what to do with themes too?

{
    "name": "ianmjones/composer-test.dev",
    "description": "Plugins for WordPress site",
    "authors": [
        {
            "name": "Ian M. Jones",
            "email": "ian@ianmjones.com"
        }
    ],
    "require": {},
    "extra": {
        "installer-paths": {
            "wp-content/plugins/{$name}/": ["type:wordpress-plugin"],
            "wp-content/themes/{$name}/": ["type:wordpress-theme"]
        }
    }
}

If your plugins and themes are in a different location then you need to update the “installer-paths” accordingly. The “wordpress-plugin” custom type is a common standard so it’s best to stick with it if you want to use Composer for other people’s WordPress plugins too.

Repositories

Because our Pro plugins are not publicly available in Packagist or GitHub we need to tell composer where and how to download our plugins.

The following snippet shows the “repositories” entry added to our composer.json file, with a couple of placeholders you will need to fill in. The “require” section now also references the newly discoverable packages from the repositories section.

{
    "name": "ianmjones/composer-test.dev",
    "description": "Plugins for WordPress site",
    "authors": [
        {
            "name": "Ian M. Jones",
            "email": "ian@ianmjones.com"
        }
    ],
    "require": {
        "deliciousbrains/wp-migrate-db-pro": "*",
        "deliciousbrains/wp-migrate-db-pro-media-files": "*",
        "deliciousbrains/wp-migrate-db-pro-cli": "*"
    },
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "deliciousbrains/wp-migrate-db-pro",
                "type": "wordpress-plugin",
                "version": "1.4.6",
                "dist": {
                    "type": "zip",
                    "url": "https://deliciousbrains.com/dl/wp-migrate-db-pro-latest.zip?licence_key=<LICENSE_KEY>&site_url=composer-test.dev"
                },
                "require": {
                    "composer/installers": "^1.0"
                }
            }
        },
        {
            "type": "package",
            "package": {
                "name": "deliciousbrains/wp-migrate-db-pro-media-files",
                "type": "wordpress-plugin",
                "version": "1.3.1",
                "dist": {
                    "type": "zip",
                    "url": "https://deliciousbrains.com/dl/wp-migrate-db-pro-media-files-latest.zip?licence_key=<LICENSE_KEY>&site_url=composer-test.dev"
                },
                "require": {
                    "composer/installers": "^1.0"
                }
            }
        },
        {
            "type": "package",
            "package": {
                "name": "deliciousbrains/wp-migrate-db-pro-cli",
                "type": "wordpress-plugin",
                "version": "1.1",
                "dist": {
                    "type": "zip",
                    "url": "https://deliciousbrains.com/dl/wp-migrate-db-pro-cli-latest.zip?licence_key=<LICENSE_KEY>&site_url=composer-test.dev"
                },
                "require": {
                    "composer/installers": "^1.0"
                }
            }
        }
    ],
    "extra": {
        "installer-paths": {
            "wp-content/plugins/{$name}/": ["type:wordpress-plugin"],
            "wp-content/themes/{$name}/": ["type:wordpress-theme"]
        }
    }
}

There’s a few things to note about our (now complete) composer.json file:

  • You must replace <LICENSE_KEY> with your WP Migrate DB Pro license key.
  • The version for each of the repository entries should be updated when a new version of the plugins are released and you’re ready to update to them.
  • The site_url entry should not include http:// or https://, just the base domain and path for your site (our example is using composer-test.dev).

Specific Versions

You can now lock your composer.json file to specific versions of the plugins, instead of having to accept the latest version. You need to specify the version in three places:

  1. The require block
  2. The repositories package version
  3. In the URL of the repositories package dist url
{ 
   "require": {
        "deliciousbrains/wp-migrate-db-pro": "<VERSION>",
    },
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "deliciousbrains/wp-migrate-db-pro",
                "type": "wordpress-plugin",
                "version": "<VERSION>",
                "dist": {
                    "type": "zip",
                    "url": "https://deliciousbrains.com/dl/wp-migrate-db-pro-<VERSION>.zip?licence_key=<LICENSE_KEY>&site_url=<SITE&gt"
                },
                "require": {
                    "composer/installers": "^1.0"
                }
            }
        },
   ]
}

composer install

With our complete composer.json file we’re ready to install the plugins.

composer install

When you check out the Plugins list in the WordPress Dashboard you should now see the plugins have been installed.

Plugins Installed

You can now activate the plugins as normal to start using them.

Updating

When a new plugin is released and you would like to update your plugins, the ‘require’ section of the composer.json file will need to be updated. This can be done by replacing “*” with the version you’re trying to update to.

"require": {
        "deliciousbrains/wp-migrate-db-pro": "1.8",
        "deliciousbrains/wp-migrate-db-pro-media-files": "1.4.8",
        "deliciousbrains/wp-migrate-db-pro-cli": "1.3.1"
    }

Once these specific versions have been added to your composer.json file, run composer update and the plugins should update.

Conclusion

There are some great tutorials on how to quickly set up a new WordPress site with Composer. Now you know how easy it is to add repositories for WP Migrate DB Pro and its addons so that you can rapidly migrate an existing WordPress database to the new site.