How to build you own plugin i n Payload.

Building your own plugin is easy, and if you’re already familiar with Payload then you’ll have everything you need to get started. You can either start from scratch or use the Payload plugin template to get up and running quickly.

Our plugin template includes everything you need to build a full life-cycle plugin:

  • Example files and functions for extending the payload config
  • A local dev environment to develop the plugin
  • Test suite with integrated GitHub workflow

By abstracting your code into a plugin, you’ll be able to reuse your feature across multiple projects and make it available for other developers to use.

To install any plugin, simply add it to your Payload config in the plugins array.

import samplePlugin from ‘sample-plugin’;

2

3const config = buildConfig({

4 plugins: [

5 // Add plugins here

6 samplePlugin({

7 enabled: true,

8 }),

9 ],

10});

11

12export default config;

Initialization

The initialization process goes in the following order:

  1. Incoming config is validated
  2. Plugins execute
  3. Default options are integrated
  4. Sanitization cleans and validates data
  5. Final config gets initialized

In the Payload plugin template, you will see a common file structure that is used across plugins:

  1. root folder – general configuration
  2. /src folder – everything related to the plugin
  3. /dev folder – sanitized test project for development

Root

In the root folder, you will see various files related to the configuration of the plugin. We set up our environment in a similar manner in Payload core and across other projects. The only two files you need to modify are:

  • README.md – This contains instructions on how to use the template. When you are ready, update this to contain instructions on how to use your Plugin.
  • package.json – Contains necessary scripts and dependencies. Overwrite the metadata in this file to describe your Plugin.

Leave a comment

Your email address will not be published. Required fields are marked *