Docs
Functions

createEventHandler

Handle a WordPress webhook event inside an extension.

createEventHandler builds a handler that runs when WordPress emits a webhook event: a post published, a term deleted, or a custom event from your own plugin. Put the result in an extension's init events array. Kizlo dispatches each incoming event to every handler that matches.

Events are one-way and fire-and-forget. The handler runs server-side after WordPress notifies Kizlo, returns nothing, and a throw is caught and logged, never surfaced to a caller.

Parameters

It takes two forms:

createEventHandler(handler)         // receive every built-in event
createEventHandler(events, handler) // declare custom events, then receive only those

events

Optional. An array declaring the custom event types this handler accepts and a schema for each one's data. Omit it to receive every built-in WordPress event instead. Each entry:

Prop

Type

handler

(event, context) => void | Promise<void>. Runs for each matching event. It receives:

  • event: the { type, data } payload, or null when the request carries no recognized event. With events declared, event is narrowed to your union, so switching on event.type types event.data. Without it, event is the built-in event shape.
  • context: a ProcedureContext.

Returns

An EventHandler. List it in an extension's init events array.

Examples

Handle built-in events by switching on event.type. Each case narrows event.data:

src/lib/kizlo/server/cache/index.ts
import { createEventHandler } from "kizlo"

export const onContentChange = createEventHandler(async (event, context) => {
  if (!event) return

  switch (event.type) {
    case "post.updated": // event.data → { post_id, post_type }
      await revalidatePath(`/posts/${event.data.post_id}`)
      break
    case "term.deleted": // event.data → { term_id, taxonomy, post_types, count }
      context.logger.info("term removed", { id: event.data.term_id })
      break
  }
})

Handle a custom event by declaring its type and data schema first, so the handler is typed to that union. Your WordPress plugin emits it with kizlo_emit_event:

import { createEventHandler } from "kizlo"
import { z } from "zod"

export const onReview = createEventHandler(
  [{ types: ["review.created"], data: z.object({ review_id: z.number(), rating: z.number() }) }],
  async (event, context) => {
    if (event?.type !== "review.created") return
    context.logger.info("new review", { rating: event.data.rating }) // data is typed
  },
)

On this page