New series: what assistive tech really hears on your checkout page. Start with part one →
Shipwright
Home
WordPress Sep 4, 2026 1 min read Updated Sep 19, 2026

Registering a REST Route the Boring, Secure Way

Tomás Herrera

Tomás Herrera

@tomasherrera_

Share
Cover illustration for the article Registering a REST Route the Boring, Secure Way

Permission callbacks, argument schemas, and nonces for a custom endpoint. WordPress rewards developers who work with its APIs instead of around them. Almost every problem we have debugged in client projects started with a shortcut that bypassed a core function.

The WordPress way

Register things on the right hook, pass complete arguments, and let core handle the rest. It is more typing up front, but the result survives updates and plays nicely with other plugins.

rest-route.php
add_action( 'rest_api_init', function () {
	register_rest_route( 'shipwright/v1', '/bookmarks/(?P<id>d+)', [
		'methods'             => 'POST',
		'callback'            => 'shipwright_save_bookmark',
		'permission_callback' => fn () => current_user_can( 'read' ),
		'args'                => [
			'id' => [ 'validate_callback' => fn ( $v ) => is_numeric( $v ) ],
		],
	] );
} );

Registering it properly

Every endpoint and form needs three things: a capability check, a nonce or equivalent, and sanitisation on the way in with escaping on the way out. Skipping any one of them is how sites get compromised.

  • Check capabilities before doing anything.
  • Verify a nonce on every state-changing request.
  • Sanitise input and escape output, always.
  • Script test sites with WP-CLI.

Security checklist

WP-CLI makes all of this faster to test. We script the setup of a fresh site for every experiment so nothing depends on the state of a long-lived development database.

Before you ship this

A permission_callback that returns true makes the route public. That is fine for read-only data, but never for anything that writes.

Programs must be written for people to read, and only incidentally for machines to execute.

Harold Abelson, Structure and Interpretation of Computer Programs, 1985

Wrapping up

Questions, corrections, and war stories are welcome in the comments. Several of our best follow-up posts started as a reader pointing out something we had missed.

Tomás Herrera

Tomás Herrera

Author

PHP and WordPress developer for fifteen years. Tomás maintains a handful of open-source plugins and a strong opinion about hooks.

Related Posts

Comments 3

Diego Alvarez

How does this hold up on older Safari versions? That is where we always get burned.

Ben Carter

Solid write-up. The warning about caching bit us hard last year.

Yuki Tanaka

Clear, practical, no fluff. More of this, please.

Leave a comment

Your email will not be published. Sign in for a faster experience.

Supports **bold**, *italic*, and `code`.