-
Notifications
You must be signed in to change notification settings - Fork 0
/
extending-rest-api.php
63 lines (39 loc) · 1.19 KB
/
extending-rest-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/*
Plugin Name: Extending Rest
Description: Oversimplified Class for making custom Rest Endpoints and extending returned data
Version: 0.1
Author: Jordan Cauley
License: GPL2
*/
class Extending_Rest {
public function __construct(){
add_action('init', array($this, 'er_add_origin_header'));
add_action( 'rest_api_init', array($this, 'er_routes'));
add_action( 'rest_api_init', array($this, 'er_add_custom_fields') );
}
function er_add_origin_header(){
header("Access-Control-Allow-Origin: *");
}
function er_sample_response( WP_REST_Request $request ){
return $_GET;
}
function er_routes(){
register_rest_route( 'er/v1', '/sample/', array(
'methods' => 'GET',
'callback' => array( $this, 'er_sample_response'),
) );
}
function er_add_custom_fields(){
register_rest_field( array('post'),
'meta',
array(
'get_callback' => function($post){
$fields = get_post_meta($post['id']);
return $fields;
}
)
);
}
}
new Extending_Rest();