-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
41 lines (36 loc) · 1014 Bytes
/
index.php
File metadata and controls
41 lines (36 loc) · 1014 Bytes
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
<?php
/**
* Plugin Name: CL Custom API Route
* Description: Exposes a custom API route for the next.js app to consume
* Version: 1.0
* Author: David Crammer
* Author URI: https://davidcrammer.com
*/
function handle_custom_route_request($request)
{
$posts = get_posts(array(
'post_type' => 'post',
'posts_per_page' => 10,
));
$formatted_posts = array_map(function ($post) {
return array(
'id' => $post->ID,
'title' => $post->post_title,
'content' => $post->post_content,
'excerpt' => $post->post_excerpt,
'slug' => $post->post_name,
'date' => $post->post_date,
'featured_image' => get_the_post_thumbnail_url($post->ID),
'audio_url' => get_post_meta($post->ID, 'enclosure', true),
);
}, $posts);
return $formatted_posts;
}
function custom_api_route()
{
register_rest_route('cl/v1', 'posts', array(
'methods' => 'GET',
'callback' => 'handle_custom_route_request',
));
}
add_action('rest_api_init', 'custom_api_route');