Plugin Development for Payload CMS
A Payload plugin is a function that takes a configuration and returns a modified configuration. It's not magic: a plugin simply adds collections, fields, hooks, endpoints, and components to the existing configuration before CMS initialization. Official plugins (@payloadcms/seo, @payloadcms/form-builder) follow this same model.
Plugin Architecture
// Plugin type
type Plugin = (incomingConfig: Config) => Config
// Simplest plugin
const myPlugin: Plugin = (config) => {
return {
...config,
collections: [
...(config.collections || []),
// add collection
],
hooks: {
...config.hooks,
afterInit: [
...(config.hooks?.afterInit || []),
// add hook
],
},
}
}
export default buildConfig({
plugins: [myPlugin],
})
SEO Plugin (Full Implementation)
// plugins/seo/index.ts
import type { Config, CollectionConfig, GlobalConfig } from 'payload/types'
interface SEOPluginConfig {
collections?: string[] // collection slugs to add SEO fields
globals?: string[]
uploadsCollection?: string
generateTitle?: (doc: any) => string
generateDescription?: (doc: any) => string
}
export const seoPlugin = (pluginConfig: SEOPluginConfig) => (config: Config): Config => {
const seoFields = [
{
name: 'meta',
type: 'group' as const,
label: 'SEO',
admin: { position: 'sidebar' as const },
fields: [
{
name: 'title',
type: 'text' as const,
admin: {
description: ({ doc }: any) =>
pluginConfig.generateTitle?.(doc) || 'Auto-fill: document title',
},
},
{
name: 'description',
type: 'textarea' as const,
maxLength: 160,
},
{
name: 'image',
type: 'upload' as const,
relationTo: pluginConfig.uploadsCollection || 'media',
},
{
name: 'noIndex',
type: 'checkbox' as const,
defaultValue: false,
},
],
},
]
return {
...config,
collections: config.collections?.map(collection => {
if (pluginConfig.collections?.includes(collection.slug)) {
return {
...collection,
fields: [...(collection.fields || []), ...seoFields],
}
}
return collection
}),
globals: config.globals?.map(global => {
if (pluginConfig.globals?.includes(global.slug)) {
return {
...global,
fields: [...(global.fields || []), ...seoFields],
}
}
return global
}),
// Add hook for auto-populate
hooks: {
...config.hooks,
afterRead: [
...(config.hooks?.afterRead || []),
({ doc }: any) => {
if (!doc.meta?.title && pluginConfig.generateTitle) {
doc.meta = {
...doc.meta,
title: pluginConfig.generateTitle(doc),
}
}
return doc
},
],
},
}
}
Usage:
// payload.config.ts
import { seoPlugin } from './plugins/seo'
export default buildConfig({
plugins: [
seoPlugin({
collections: ['posts', 'pages', 'products'],
globals: ['home-page'],
uploadsCollection: 'media',
generateTitle: (doc) => `${doc.title} | My Site`,
generateDescription: (doc) => doc.excerpt || '',
}),
],
})
Audit Log Plugin
// plugins/audit-log/index.ts
import type { Config } from 'payload/types'
interface AuditLogConfig {
collections: string[]
}
export const auditLogPlugin = ({ collections }: AuditLogConfig) => (config: Config): Config => {
// Collection for storing logs
const auditCollection = {
slug: 'audit-logs',
admin: { hidden: true },
access: {
read: ({ req }: any) => req.user?.role === 'admin',
create: () => false, // only via API
update: () => false,
delete: () => false,
},
fields: [
{ name: 'collection', type: 'text' as const },
{ name: 'docId', type: 'text' as const },
{ name: 'operation', type: 'text' as const },
{ name: 'user', type: 'relationship' as const, relationTo: 'users' as const },
{ name: 'before', type: 'json' as const },
{ name: 'after', type: 'json' as const },
{ name: 'timestamp', type: 'date' as const },
],
}
// Hooks for tracked collections
const auditedCollections = config.collections?.map(collection => {
if (!collections.includes(collection.slug)) return collection
return {
...collection,
hooks: {
...collection.hooks,
afterChange: [
...(collection.hooks?.afterChange || []),
async ({ doc, previousDoc, operation, req }: any) => {
if (!req.payload) return
await req.payload.create({
collection: 'audit-logs',
data: {
collection: collection.slug,
docId: String(doc.id),
operation,
user: req.user?.id,
before: previousDoc || null,
after: doc,
timestamp: new Date().toISOString(),
},
disableVerificationEmail: true,
})
},
],
},
}
})
return {
...config,
collections: [
...(auditedCollections || []),
auditCollection,
],
}
}
Plugin with Custom Endpoints
// plugins/search/index.ts
export const searchPlugin = (config: Config): Config => ({
...config,
endpoints: [
...(config.endpoints || []),
{
path: '/search',
method: 'get' as const,
handler: async (req: any, res: any) => {
const { q } = req.query
if (!q) return res.json({ docs: [] })
const results = await Promise.all([
req.payload.find({
collection: 'posts',
where: { or: [{ title: { like: q } }, { excerpt: { like: q } }] },
limit: 5,
}),
req.payload.find({
collection: 'products',
where: { name: { like: q } },
limit: 5,
}),
])
return res.json({
docs: [
...results[0].docs.map(d => ({ ...d, _type: 'post' })),
...results[1].docs.map(d => ({ ...d, _type: 'product' })),
],
})
},
},
],
})
Publishing Plugin as npm Package
// Plugin package.json
{
"name": "@myorg/payload-plugin-seo",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"peerDependencies": {
"payload": "^2.0.0"
},
"scripts": {
"build": "tsc"
}
}
// src/index.ts
export { seoPlugin } from './plugin'
export type { SEOPluginConfig } from './types'
Plugin Testing
// tests/plugin.test.ts
import { buildConfig } from 'payload/config'
import { seoPlugin } from '../src'
describe('SEO Plugin', () => {
it('should add SEO fields to specified collections', () => {
const baseConfig = buildConfig({
collections: [{ slug: 'posts', fields: [{ name: 'title', type: 'text' }] }],
plugins: [seoPlugin({ collections: ['posts'] })],
})
const postsCollection = baseConfig.collections.find(c => c.slug === 'posts')
const metaField = postsCollection?.fields.find((f: any) => f.name === 'meta')
expect(metaField).toBeDefined()
expect(metaField?.type).toBe('group')
})
})
Timeline
Developing one reusable plugin (SEO, audit, search) with tests takes 3–5 days.







