How to Register a Simple Custom WordPress REST API Endpoint

When I start a custom integration, I first prove the wiring works. Here is how to register a simple custom wordpress rest api endpoint with a tiny GET route that returns 1—enough to confirm your namespace is live before you add real data or auth.

How to register a simple custom WordPress REST API endpoint

Why learn how to register a simple custom WordPress REST API endpoint

Core already exposes posts, media, and users under /wp-json/. Your own plugin or theme work usually needs a private namespace. A one-line callback tells you permalinks, REST, and PHP are healthy without debugging business logic. Same “small hook first” habit as other WordPress PHP snippets I keep for admin and front-end tweaks.

Before and after how to register a simple custom wordpress rest api endpoint

The test endpoint code

Drop this in a child theme functions.php, a small plugin, or a snippets plugin. After saving, open https://yoursite.com/wp-json/wso/v1/test (or hit it with curl).

/*
 * Test Rest API End Point
 */
add_action( 'rest_api_init', function () {
	// site_url . 'wp-json/wso/v1/test'
	register_rest_route( 'wso/v1', '/test/', array(
		'methods'  => 'GET',
		'permission_callback' => '__return_true',
		'callback' => function ( WP_REST_Request $request ) {
			return '1';
		},
	) );
} );

What each part does

  • rest_api_init — Fires when WordPress builds the REST server; register routes here, not on init.
  • register_rest_route( 'wso/v1', '/test/', … ) — Namespace wso/v1 plus route /test/ → URL /wp-json/wso/v1/test.
  • methods => 'GET' — Only GET is allowed for this smoke test.
  • permission_callback => '__return_true' — Public access. Fine for a local test; lock this down before production.
  • callback — Receives WP_REST_Request; returning '1' becomes the JSON response body.

Prefer a Code Snippets Plugin?

If you would rather not touch theme files, park this route in WPCode, Code Snippets, or FluentSnippets.

Add a PHP snippet, paste the rest_api_init block, and run it on the front end (REST requests are front-end style). Name it something like “WSO test REST route” so you can turn it off after smoke-testing.

Before going live, change permission_callback to a real capability check—or delete the snippet entirely so the open test URL disappears.

PHP register_rest_route for how to register a simple custom wordpress rest api endpoint

How to test it

  • Visit /wp-json/wso/v1/test in the browser—you should see 1 (often quoted as JSON).
  • Or: curl -sS https://yoursite.com/wp-json/wso/v1/test
  • 404? Flush permalinks (Settings → Permalinks → Save) and confirm the snippet/plugin is active.
  • Official reference: Adding custom endpoints.

FAQ

Is __return_true safe on a live site?

Only for throwaway tests. Use current_user_can( 'manage_options' ) or an application-password / nonce check for real endpoints.

Can I return JSON arrays or objects?

Yes. Return a PHP array/object (or WP_REST_Response); WordPress encodes it as JSON automatically.

Why wso/v1?

It is just a custom namespace string. Use your project prefix so you do not collide with core or other plugins.

Does this need pretty permalinks?

Pretty permalinks make /wp-json/… nicer. With plain permalinks, REST still works via ?rest_route=/wso/v1/test.

Wrap-up

That is how to register a simple custom WordPress REST API endpoint for smoke tests: hook rest_api_init, register_rest_route under your namespace, allow GET, return a trivial payload, then tighten permissions when you build the real API.

Leave a Comment

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

Scroll to Top