Extending wp/v2

Put your data on core’s routes with show_in_rest and registered meta, and know when a new route is the better answer.

4 lessons16 interview questions

No new route. Core already has one. A custom post type with show_in_rest gets /wp/v2/faq for free. Your meta can ride along, or stay private. In this module you decide which, on real 4WP FAQ and 4WP TODO code.

CourseWordPress REST API Course ยท Module 3 of 9 ยท Stage 2 "Build your own"
Learnshow_in_rest on post types and taxonomies, register_post_meta / register_term_meta with a schema, auth_callback
HowReal code from 4WP FAQ and 4WP TODO, before/after JSON responses, interview questions after every lesson

Property? Extend. Resource? Own namespace.

flowchart TD
  Q{"Is it a property<br/>of a post or term?"}
  Q -- "yes" --> E["show_in_rest<br/>register_post_meta"]
  Q -- "no, a resource" --> N["own namespace<br/>forwp-faq/v1/settings"]
  E --> P{"Should clients see it?"}
  P -- "no" --> H["show_in_rest => false<br/>+ auth_callback"]
  classDef yes fill:#e8f6f0,stroke:#49cc90,color:#1e293b
  classDef no fill:#ebf3fb,stroke:#61affe,color:#1e293b
  class E,H yes
  class N no

show_in_rest is a publish button. Press it only for data a client should see.

Before โ†’ after

BeforeAfter Module 3
A new route for every field"meta": { "answers": [ โ€ฆ ] } on /wp/v2/faq/{id}
Meta that anyone can writesanitize_callback + auth_callback
Private notes visible in RESTshow_in_rest => false for comment meta
Settings forced into a post typea separate resource in your own namespace

You'll be able to answer

  • How do you expose post meta in the REST API, and what does the schema give you?
  • What is auth_callback in register_meta for?
  • When is extending wp/v2 the wrong choice?

Lessons in this module 4 lessons