Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/andrewleedham/wordpress-rest-api-extended

Extends WordPress' REST API, allowing the Content-Type header to be specified.
https://github.com/andrewleedham/wordpress-rest-api-extended

Last synced: 8 days ago
JSON representation

Extends WordPress' REST API, allowing the Content-Type header to be specified.

Awesome Lists containing this project

README

        

# wordpress-rest-api-extended
Extends WordPress' REST API, allowing the Content-Type header to be specified, and 404 errors to be raised.

## Usage
Register a route that just returns HTML.
```php
register_rest_route( 'namespace/v1', 'route' , array(
'methods' => 'GET',
'callback' => function(){return '

Hello World!

';}, // Callback can return a string that will be passed as the response.
'content_type' => 'text/html', // Specify any custom Content-Type.
'json' => false // Route content is parsed as JSON by default, so must be disabled.
) );
```

register_rest_route will still work as normal with JSON:
```php
register_rest_route( 'namespace/v1', 'route' , array(
'methods' => 'GET',
'callback' => function(){return array('json': 'value');}
) );

// or explicitly:
register_rest_route( 'namespace/v1', 'route' , array(
'methods' => 'GET',
'callback' => function(){return array('json': 'value');},
'content_type' => 'application/json',
'json' => true
) );
```
Register a route that just returns a 404 error.
```php
register_rest_route( 'namespace/v1', 'route' , array(
'methods' => 'GET',
'callback' => function(){return false;}, // Callback can return a false to trigger a 404 error.
'content_type' => 'text/html', // Errors will be returned as JSON regardless.
'json' => false
) );
```