salferrarello.com/slides/wp-rest-api-fun
( Jump to Resources )
{
"name": {
"first": "Sal",
"last": "Ferrarello"
},
"wcus": [
2015, 2016
]
}
https://salferrarello.com/wp-json/salcode/v1/owner
await wp.apiFetch({
data: {
title: 'My new title',
},
method: 'POST',
path: 'wp/v2/pages/8926'
});
![]() |
![]() |
{
"name": {
"first": "Sal",
"last": "Ferrarello"
},
"wcus": [
2015, 2016
]
}
https://salferrarello.com/wp-json/salcode/v1/owner
JSON | PHP |
---|---|
|
|
{"name":{"first":"Sal","last":
"Ferrarello"},"wcus":[2015,2016]}
https://salferrarello.com/wp-json/salcode/v1/owner
{"name":{"first":"Sal","last":
"Ferrarello"},"wcus":[2015,2016]}
https://salferrarello.com/wp-json/salcode/v1/owner
{
"name": {
"first": "Sal",
"last": "Ferrarello"
},
"wcus": [
2015, 2016
]
}
https://salferrarello.com/wp-json/salcode/v1/owner
Chrome browser extension to pretty print JSON
See JSONVue in Chrome Web store
While not as convenient as a browser extension, you can use this JS trick
await (await fetch(
window.location.href
)).json()
See Pretty Print JSON URL blog post
How to find blog post REST API URL
<link
rel="alternate"
type="application/json"
href="https://salferrarello.com/wp-json
/wp/v2/posts/6799"
/>
document.querySelector(
'link[rel="alternate"]' +
'[type="application/json"]'
).href
(await (await fetch(
'/wp-json/wp/v2/posts/6799'
)).json())
jsonData = (await (await fetch(
'/wp-json/wp/v2/posts/6799'
)).json());
jsonData.modified
Web browser JavaScript can only access URLs on the same domain
https://salferrarello.com/wp-json/wp/v2/posts/6799
https://example.com/wp-json/
wp/v2/post/6799
wp/v2/post/6799
wp/v2
salcode/v1/owner
salcode/v1
salcode-v1/owner
salcode-v1
salcodev1/owner
salcodev1
tuna/owner
tuna
salcode/v1/owner
salcode/v1
https://salferrarello.com/wp-json/wp/
wp/v2
(await (await fetch(
'/wp-json/wp-site-health/v1/' +
'tests/background-updates'
)).json());
(await (await fetch(
'/wp-json/wp-site-health/v1/' +
'tests/background-updates'
)).json());
(await (await fetch(
'/wp-json/wp-site-health/v1/' +
'tests/background-updates',
{
headers: { 'X-Wp-Nonce': 'd2bd6ec6c3' },
}
)).json());
On /wp-admin
pages available from JavaScript global
wpApiSettings.nonce
Also available at the URL
/wp-admin/admin-ajax.php?action=rest-nonce
await wp.apiFetch({
path: '/wp-site-health/v1/' +
'tests/background-updates'
})
await wp.apiFetch({
data: {
title: 'My new title',
},
method: 'POST',
path: 'wp/v2/pages/8926'
});
Read | Write |
---|---|
|
|
add_action(
'rest_api_init',
'salcode_owner_rest'
);
https://github.com/salcode/owner-rest-endpoint
function salcode_owner_rest() {
register_rest_route(
'salcode/v1', '/name',
[
'callback' => 'salcode_owner_callback',
'permission_callback' =>
'__return_true',
]
);
}
https://github.com/salcode/owner-rest-endpoint
function salcode_owner_callback() {
return [
'name' =>
[
'first' => 'Sal',
'last' => 'Ferrarello'
],
'wcus' => [ 2015, 2016 ],
];
}
https://github.com/salcode/owner-rest-endpoint
{
"name": {
"first": "Sal",
"last": "Ferrarello"
},
"wcus": [
2015, 2016
]
}
https://salferrarello.com/wp-json/salcode/v1/owner
register_rest_route(
'salcode/v1', '/name',
[
'callback' => 'salcode_owner_callback',
'permission_callback' =>
'__return_true',
]
);
functions.php
mu-plugins
directory/wp-content/mu-plugins/
/wp-content/mu-plugins/owner-rest-endpoint.php
MyWeather Plugin adds REST endpoint with weather