רישום endpoint מותאם ב-REST API בכדי לחשוף מידע מהתבנית או מהתוסף שלכם. לעוד על אבטחת REST API.
add_action( 'rest_api_init', function () {
register_rest_route( 'savvy/v1', '/recent-posts/', array(
'methods' => 'GET',
'callback' => 'savvy_get_recent_posts',
'permission_callback' => '__return_true',
) );
} );
function savvy_get_recent_posts() {
$posts = get_posts( array(
'numberposts' => 5,
'post_status' => 'publish',
) );
$data = array();
foreach ( $posts as $post ) {
$data[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'link' => get_permalink( $post ),
);
}
return rest_ensure_response( $data );
}הוסיפו ל-functions.php. הגישה דרך /wp-json/savvy/v1/recent-posts/. הגדירו permission_callback בכדי להגביל גישה במידת הצורך.