Igor Simic
4 years ago

Wordpress REST API set custom headers




How to set custom headers when returning REST API data in Wordpress?
To achieve this goal we will use WP_REST_Response class, because this class can allow us to set custom headers.
In our case we will set X-WP-Total & X-WP-TotalPages used for pagination:

$response = new WP_REST_Response($data, 200);

            $response->header( 'X-WP-Total', $total ); // total = total number of post
            $response->header( 'X-WP-TotalPages', $max_pages ); // maximum number of pages
   
            return $response;