Action Hook since 4.4.0

rest_api_init

Fires when the REST API is initialized — register custom routes here

Description

rest_api_init is the register_rest_route() equivalent of init — it only fires on REST requests, not every page load.

When it runs

Only on requests that hit the REST API (/wp-json/...), not on regular front-end or admin page loads.

Signature

do_action( 'rest_api_init', WP_REST_Server $wp_rest_server );

Parameters

  • $wp_rest_server WP_REST_Server — The REST server instance handling the request.

Examples

Basic

add_action( 'rest_api_init', function() {
    register_rest_route( 'my-plugin/v1', '/books', [
        'methods'  => 'GET',
        'callback' => 'my_plugin_get_books',
        'permission_callback' => '__return_true',
    ] );
} );

Register a custom read-only REST endpoint.

Common Use Cases

  • Register custom REST routes
  • Expose extra fields on existing endpoints via register_rest_field
  • Add custom REST authentication logic

Common mistakes

  • Registering routes on init instead — they'd run on every request for no reason, and some REST internals aren't ready yet

Related hooks

FAQ

Why does register_rest_route() have to run on rest_api_init specifically?

The REST server object doesn't exist yet on earlier hooks like init; rest_api_init is where WordPress builds it before dispatching the request.

Source: wp-includes/rest-api.php

Group wizard

One session: this hook, then each related_hooks entry. Walk it and tell us what to change.

rest_api_init → rest_prepare_post

functions.php — rest_api_init
functions.php
PHP rest_api_init · Hook 1 of 2 · Step 1 of 6

Practice the rest_api_init WordPress action hook in our interactive sandbox — a VS Code-style editor with a step-by-step rest_api_init tutorial, add_action exercises, and instant feedback. No local WordPress install required.