A REST API is a way for different programs to talk to each other over the internet using simple commands like GET, POST, PUT, and DELETE. It works like ordering food at a restaurant: you send a request, and you get a response.
Click to reveal answer
beginner
How do you fetch data from a WordPress REST API using JavaScript?
You use the fetch() function with the WordPress REST API URL. For example, fetch('https://example.com/wp-json/wp/v2/posts') gets the list of posts in JSON format.
Click to reveal answer
beginner
What HTTP method do you use to create a new post via the WordPress REST API?
You use the POST method to send new data to the server and create a new post.
Click to reveal answer
intermediate
Why do you need to include authentication when modifying WordPress data via REST API?
Because changing data like creating or deleting posts needs permission. Authentication proves you have the right to make those changes.
Click to reveal answer
beginner
What is the purpose of the 'wp-json' part in the WordPress REST API URL?
The 'wp-json' part tells WordPress to use its REST API system to send or receive data in JSON format.
Click to reveal answer
Which JavaScript function is commonly used to call a REST API?
Afetch()
Balert()
Cconsole.log()
DsetTimeout()
✗ Incorrect
fetch() is used to make network requests, including calling REST APIs.
What HTTP method is used to update existing data in a REST API?
AGET
BPOST
CPUT
DDELETE
✗ Incorrect
PUT is used to update existing data on the server.
In WordPress REST API, what does the endpoint '/wp-json/wp/v2/posts' return?
AList of pages
BList of posts
CList of comments
DList of users
✗ Incorrect
This endpoint returns the list of posts in WordPress.
Why is authentication important when using REST API to modify WordPress content?
ATo avoid using HTTPS
BTo speed up the request
CTo get data faster
DTo prove permission to change data
✗ Incorrect
Authentication proves you have permission to make changes.
Which format does WordPress REST API use to send data?
AJSON
BXML
CCSV
DHTML
✗ Incorrect
WordPress REST API uses JSON format for data exchange.
Explain how you would use JavaScript to get a list of posts from a WordPress site using the REST API.
Think about sending a request and reading the response.
You got /4 concepts.
Describe why authentication is necessary when creating or deleting posts via the WordPress REST API.
Consider site security and permissions.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using the WordPress REST API with JavaScript?
easy
A. To get or send data to the WordPress server without reloading the page
B. To change the WordPress theme colors
C. To install WordPress plugins automatically
D. To create new WordPress users manually
Solution
Step 1: Understand REST API role
The REST API allows JavaScript to communicate with the server asynchronously.
Step 2: Identify the purpose in WordPress context
It is used to get or send data like posts or pages without reloading the page.
Final Answer:
To get or send data to the WordPress server without reloading the page -> Option A
Quick Check:
REST API = Data exchange without reload [OK]
Hint: REST API means data exchange without page reload [OK]
Common Mistakes:
Thinking REST API changes themes or plugins
Confusing REST API with WordPress admin tasks
Assuming REST API reloads the page
2. Which of the following is the correct way to fetch posts from the WordPress REST API using JavaScript?
easy
A. fetch('/wp-json/wp/v2/posts').then(response => response.json())
B. fetch('wp-json/wp/v2/posts').json()
C. fetch('/wp-json/wp/v2/posts').json()
D. fetch('/wp-json/wp/v2/posts').then(response => response.text())
Solution
Step 1: Check fetch URL format
The URL must start with a slash '/' to indicate root path: '/wp-json/wp/v2/posts'.
Step 2: Use correct response method
Use response.json() inside a then() to parse JSON data correctly.
Final Answer:
fetch('/wp-json/wp/v2/posts').then(response => response.json()) -> Option A
D. Error: Cannot read property 'rendered' of undefined
Solution
Step 1: Understand fetch and JSON parsing
The fetch gets posts, then res.json() parses the JSON array of posts.
Step 2: Loop through posts and log titles
posts.forEach logs each post's title.rendered, so it prints 'Hello' and 'World'.
Final Answer:
Hello\nWorld -> Option C
Quick Check:
posts.forEach + post.title.rendered = Hello and World [OK]
Hint: Use console.log inside forEach to print each post title [OK]
Common Mistakes:
Assuming posts is not an array
Forgetting to parse JSON with res.json()
Accessing title without .rendered property
4. Identify the error in this code snippet that tries to fetch pages from WordPress REST API:
async function getPages() {
const response = await fetch('wp-json/wp/v2/pages');
const data = await response.json;
console.log(data);
}
medium
A. The function should not be async
B. The fetch URL is missing a leading slash '/' and response.json is missing parentheses
C. console.log should be before fetch call
D. response.json should be replaced with response.text
Solution
Step 1: Check fetch URL format
The URL 'wp-json/wp/v2/pages' is missing a leading slash '/' which is required for root-relative path.
Step 2: Check response.json usage
response.json is a function and must be called with parentheses: response.json()
Final Answer:
The fetch URL is missing a leading slash '/' and response.json is missing parentheses -> Option B
Quick Check:
URL slash + response.json() needed [OK]
Hint: Always add '/' before REST API path and call .json() as function [OK]
Common Mistakes:
Forgetting parentheses on response.json
Missing leading slash in URL
Misplacing console.log before data is fetched
5. You want to fetch posts and display only those with the word 'JavaScript' in the title using async/await. Which code correctly filters and logs these posts?
hard
A. async function fetchJSPosts() {
const res = await fetch('/wp-json/wp/v2/posts');
const posts = await res.text();
const filtered = posts.filter(post => post.title.rendered.includes('JavaScript'));
filtered.forEach(post => console.log(post.title.rendered));
}
B. async function fetchJSPosts() {
const res = await fetch('/wp-json/wp/v2/posts');
const posts = await res.json;
const filtered = posts.filter(post => post.title.includes('JavaScript'));
filtered.forEach(post => console.log(post.title.rendered));
}
C. async function fetchJSPosts() {
const res = await fetch('/wp-json/wp/v2/posts');
const posts = await res.json();
const filtered = posts.filter(post => post.title.rendered == 'JavaScript');
filtered.forEach(post => console.log(post.title.rendered));
}
D. async function fetchJSPosts() {
const res = await fetch('/wp-json/wp/v2/posts');
const posts = await res.json();
const filtered = posts.filter(post => post.title.rendered.includes('JavaScript'));
filtered.forEach(post => console.log(post.title.rendered));
}
Solution
Step 1: Correctly fetch and parse JSON
Use await res.json() to parse the response as JSON array of posts.
Step 2: Filter posts by checking if title.rendered includes 'JavaScript'
Use posts.filter with post.title.rendered.includes('JavaScript') to select matching posts.
Step 3: Log filtered post titles
Use forEach to log each filtered post's title.rendered.
Final Answer:
async function fetchJSPosts() {
const res = await fetch('/wp-json/wp/v2/posts');
const posts = await res.json();
const filtered = posts.filter(post => post.title.rendered.includes('JavaScript'));
filtered.forEach(post => console.log(post.title.rendered));
} -> Option D