Quick start: developing a Craft 3 plugin

jake’s headshot By Jake, 13 Jan 2020

Creating a plugin for Craft CMS can be intimidating, but if you’re comfortable developing with Craft, we believe you’re 100% ready to take the leap. The following guide is a step-by-step process for creating and installing a Craft 3 plugin in just minutes.

Creating your plugin

Step #1: Create a plugin skeleton

Plugin Factory is a tool that allows users to create a basic plugin framework that you can build on, and we recommend using it as a starting point. If you’d rather build your plugin from scratch, you can refer to the Craft docs to set up the basic plugin structure.

Step #2: Register your plugin with Composer

When developing a plugin you’ll want to work on it locally, without registering it with something like Packagist, so that you don’t have to push releases every time you make a change. To install a local plugin as a package you’ll need to make a few changes to your composer.json file. Add the following to your composer.json (replacing the ‘url’ with the relative path to the plugin):

{
'minimum-stability': 'dev',
'prefer-stable': true,
'repositories': [
{
'type': 'path',
'url': './plugins/my-plugin-name'
}
]
}

Step #3: Require your plugin

While Composer now knows where to find your plugin, it still hasn’t been installed by Composer. We can ‘require’ our plugin with Composer, just like we would a published package using the plugin’s package name. To find the package name look at the composer.json file inside of your plugin folder. The name property is the package name. Once you’ve found the package name, run composer require package-name to add your plugin.

Tip: Your plugin’s name should look something like this: simplygoodwork/my-plugin-name

Because of the configuration we added in the above step, Composer will create a symlink to your plugin instead of attempting to download it from the registry like it normally would.

Step #4: Install your plugin

Your plugin should now be successfully installed by Composer, but we still need to install it with Craft. Login to your Control Panel, navigate to Settings > Plugins, and install the plugin.

Tip: To test whether your plugin is working, place some “dump and die” code within your plugin’s init method, like this:

/* File: ./plugins/barracuda/src/Barracuda.php */

// ...

public function init() {
print('Testing plugin!');
die();
}

// ...

If your plugin is properly installed it should show ​“Testing plugin!” on a blank page when you try to visit your website or Control Panel.

Step #5: Profit

You’re done! When you’re ready to publish your plugin to be publicly accessible, follow this guide in the Craft docs.

Plugins allow you to extend the core functionality of your Craft website, and there are hundreds already available through Craft’s plugin store (including some of our favorites). However, there are times when creating your own plugin can not only solve your own needs, but also provide a valuable tool for other Craft websites. Hopefully this guide kickstarts that process!


Further reading