Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // -----------------------------------------
- // stack overflow
- // -----------------------------------------
- class my_api {
- public function __construct() {
- // -----------------------------------------
- // rest routes
- // -----------------------------------------
- // version
- add_action('rest_api_init', function () {
- $version = '1';
- $namespace = 'myapi/v' . $version;
- // get posts
- register_rest_route($namespace, '/posts', array(
- 'methods' => WP_REST_Server::READABLE,
- 'permission_callback' => array($this, 'auth_user'),
- 'callback' => array($this, 'posts'),
- ));
- });
- }
- // -----------------------------------------
- // endpoints
- // -----------------------------------------
- public function posts($request) {
- $output = 'Latest Posts';
- return new WP_REST_Response($output, 200);
- }
- // -----------------------------------------
- // check nonce to verify user
- // -----------------------------------------
- public function auth_user() {
- // get nonce
- $nonce = isset($_SERVER['HTTP_X_WP_NONCE']) ? $_SERVER['HTTP_X_WP_NONCE'] : '';
- // verfiy nonce
- $nonce = wp_verify_nonce($nonce, 'wp_rest');
- // return
- return $nonce;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement