-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-rest-api-hello-world.php
58 lines (47 loc) · 1.31 KB
/
wp-rest-api-hello-world.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
<?php
/*
Plugin Name: WP REST API Hello World
Plugin URI: https://github.com/kjbenk/wp-rest-api-hello-world
Description: A Hello World example for the WordPress REST API.
Version: 0.1.0-dev
Author: Kyle Benk
Author URI: http://kylebenk.com
*/
if ( ! class_exists( 'WP_REST_WPRAHW_Controller' ) ) :
class WP_REST_WPRAHW_Controller extends WP_REST_Controller {
public function __construct() {
$this->namespace = 'wprahw';
$this->rest_base = 'hello-world';
}
/**
* Register the routes for the objects of the controller.
*/
public function register_routes() {
register_rest_route( 'wprahw', '/hello-world', array(
'methods' => 'GET',
'callback' => array( $this, 'hello_world' ),
) );
}
/**
* Output Hello World in JSON format
* @param array $data The data passed in the request
* @return json The returned JSON
*/
public function hello_world( $data ) {
return 'Hello World';
}
}
endif;
// Check to see if the function exists
if ( ! function_exists( 'wprahw_add_endpoint' ) ) :
/**
* Adds a new rest api endpoint to output hello world
*
* @link http://v2.wp-api.org/extending/adding/
*/
function wprahw_add_endpoint() {
$wprahw = new WP_REST_WPRAHW_Controller;
$wprahw->register_routes();
}
add_action( 'rest_api_init', 'wprahw_add_endpoint' );
endif;