Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/andrewleedham/wordpress-rest-api-extended
- Owner: AndrewLeedham
- License: mit
- Created: 2018-06-12T15:03:42.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-03T13:33:53.000Z (over 6 years ago)
- Last Synced: 2024-10-07T23:41:21.899Z (29 days ago)
- Language: PHP
- Size: 24.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
) );
```