Skip to main content

Preview

The client gets the content of each preview from one endpoint. The code lives in sources/server/Preview/. The route, the schema contract and the middlewares come from the REST library.

Classes​

ClassRole
ModulePublishes Config, Rest\Repository and Rest\Controller. Registers the route.
Rest\SchemaThe schema of the request. WordPress validates the request with it.
Rest\ControllerFinds the post of the URL and answers with the payload.
Rest\RepositoryBuilds the payload and keeps it in the object cache.
Rest\ExcerptMakes the fallback excerpt from the content.

Request​

GET /wp-json/rinkukado/v1/preview?url=<absolute-url>

The url parameter is the absolute URL of a post of this site.

Response​

The endpoint answers 200 with id, title, excerpt, permalink and thumbnail. The thumbnail field is null, or an object with src, srcset, width, height and alt.

{
"id": 42,
"title": "Hello world",
"excerpt": "The first words of the post…",
"permalink": "https://example.com/hello-world/",
"thumbnail": {
"src": "https://example.com/wp-content/uploads/hello.jpg",
"srcset": "…",
"width": 300,
"height": 200,
"alt": ""
}
}

The rinkukado.rest.payload filter can change the payload. See Filters.

Errors​

Every rejection answers 404 with the code not_found. The response never says which check failed.

Cache​

RequestHeader
anonymousCache-Control: public, max-age=300
from a logged-in userCache-Control: no-store

The Caching middleware of the REST library sets the header.

The client also caches each settled result for the page session. See Preview.

Filters​

FilterDefaultPurpose
rinkukado.rest.post-typesevery public type except attachmentThe types that the endpoint accepts.
rinkukado.rest.excerpt-length30The word count of the fallback excerpt.
rinkukado.rest.payloadthe payloadThe last change before the response.

Post types​

The default list of post types excludes attachment, so a media page shows no preview. Add attachment back through rinkukado.rest.post-types when a site wants a preview for its media pages.

Payload​

rinkukado.rest.payload receives the payload array and the WP_Post object, just before the endpoint sends the response.