Creating a plugin and installing it through composer

Hey guys.

So I though I was getting comfortable with Composer, but I guess I was wrong. I bought the screencast, read the documentation and the blog posts, but I still get lost in the JSON at times. I created a plugin, which works fine, but I can´t seem to get the composer.json right. I published the plugin on Github and is now trying to figure out how to install it in my WP build. The following works fine:

{
"repositories": [
    {
        "type": "composer",
        "url": "http://wpackagist.org"
    },
    {
        "type": "package",
        "package": {
            "type": "webroot",
            "name": "wordpress/wordpress",
            "version": "3.8.0",
            "dist": {
                "url": "https://github.com/WordPress/WordPress/archive/3.8.zip",
                "type": "zip"
            },
            "require": {
                "fancyguy/webroot-installer": "1.0.0"
            }
        }
    },
    {
        "type": "wordpress-plugin",
        "url": "https://github.com/StenW/WP-FAQ-plugin",
        "require": {
            "composer/installers": "1.0.*"
        }
    }
],
"require": {
    "wordpress/wordpress": "3.8.0",
    "StenW/WP-FAQ-plugin": "1.0.0",
    "wpackagist/wp-migrate-db": "*",
    "wpackagist/nextgen-gallery": "2.0.40",
    "wpackagist/maintenance-mode": "5.4",
    "wpackagist/reveal-ids-for-wp-admin-25": "1.4.5",
    "wpackagist/divup-content": "*",
    "wpackagist/search-and-replace": "2.6.5",
    "wpackagist/WP-DB-Backup": "2.2.4",
    "wpackagist/wp-bootstrap-carousel": "0.2.1",
    "wpackagist/contact-form-7": "3.6.0"
},
"extra": {
    "installer-paths": {
        "app/plugins/{$name}/": ["type:wordpress-plugin"]
    },
    "webroot-dir": "wordpress",
    "webroot-package": "wordpress/wordpress"
}

}

When I run composer update I get the following error: Repository type is not registered: wordpress-plugin
But for some reason I can´t figure out how to include my own plugin… Any help would be deeply appreciated!

I haven’t actually tried the composer route yet (so, hearty grain of salt here) but I think you need to add another repository object, pointing at your github repo (since wp-packagist is unaware of your plugin). Just like the wordpress repository object in your existing file.

Im tried that, but its trickier then one might want it to be. Il keep working on it and update the question as I go.

Check out these sections in the composer docs: http://getcomposer.org/doc/02-libraries.md#publishing-to-a-vcs and http://getcomposer.org/doc/05-repositories.md#vcs

I could go that route and install the plugin as I have done with the Wordpress core, but I believe I should use the Composer/installers. If I can´t find a solution to the issue, il do that.

So this works, I am not sure though its the right way to do it. I installed it as a third party plugin, even though I am in control of the repo.

{
"repositories": [
    {
        "type": "composer",
        "url": "http://wpackagist.org"
    },
    {
        "type": "package",
        "package": {
            "type": "webroot",
            "name": "wordpress/wordpress",
            "version": "3.8.0",
            "dist": {
                "url": "https://github.com/WordPress/WordPress/archive/3.8.zip",
                "type": "zip"
            },
            "require": {
                "fancyguy/webroot-installer": "1.0.0"
            }
        }
    },
    {
        "type": "package",
        "package": {
            "name": "StenW/WP-FAQ-plugin",
            "version": "1.0.0",
            "type": "wordpress-plugin",
            "dist": {
                "type": "zip",
                "url": "https://github.com/StenW/WP-FAQ-plugin/archive/1.0.zip"
            },
            "require": {
                "composer/installers": "1.0.7"
            }
        }
    }
],
"require": {
    "wordpress/wordpress": "3.8.0",
    "StenW/WP-FAQ-plugin": "1.0.0",
    "wpackagist/wp-migrate-db": "*",
    "wpackagist/nextgen-gallery": "2.0.40",
    "wpackagist/maintenance-mode": "5.4",
    "wpackagist/reveal-ids-for-wp-admin-25": "1.4.5",
    "wpackagist/divup-content": "*",
    "wpackagist/search-and-replace": "2.6.5",
    "wpackagist/WP-DB-Backup": "2.2.4",
    "wpackagist/wp-bootstrap-carousel": "0.2.1",
    "wpackagist/contact-form-7": "3.6.0"
},
"extra": {
    "installer-paths": {
        "app/plugins/{$name}/": ["type:wordpress-plugin"]
    },
    "webroot-dir": "wordpress",
    "webroot-package": "wordpress/wordpress"
}

}

That’s mostly correct. You have a few other options:

  1. Publish it to the WP Plugin directory just require it as wpackagist/wp-faq-plugin (I assume it keeps your composer.json file intact)
  2. Publish it to the real Packagist and just require it as stenw/wp-faq-plugin (you’d remove the custom repository as well)
  3. Keep what you have now if you don’t want to publish it anywhere but you should be able to remove the extra require with composer/installers since you’re already doing that in the composer.json… not sure if its carries though if you use a custom repository. Worth a try!

Just to improve on #3, you can change the repository you added from the package type to vcs and you’d only (pretty sure) need this information:

{
  "type": "vcs",
  "url": "https://github.com/StenW/WP-FAQ-plugin"
}

Option 4 - Look at something like Satis that will allow you to create your own “packagist” or repo manager. It’s not too bad to setup if you’ve already got the repos.

I know this is an old topic, but I still ran into issues here and hit upon this topic a few times before I got it all sorted. In case anyone else is in the same boat, here’s what I ended up doing:

  1. Go to http://packagist.org/ and copy the “Publishing Packages” code into a composer.json file in your github plugin’s repository.
  2. Register with Packagist, and then click the big green Submit Package button.
  3. Link up your github repo. You’ll have to setup your repo to automatically push to packagist, but it tells you how.
  4. That’s it, now just add your plugin like you would any other package. A single line in requirements should do it, and Packagist even tells you what to put. Also, the version number should be dev-master, not just master.

Hope that helps.

5 Likes