0
0
Wordpressframework~20 mins

REST API with JavaScript in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
REST API JavaScript Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this fetch call output in the console?

Consider this JavaScript code snippet in a WordPress theme fetching posts via REST API:

fetch('https://example.com/wp-json/wp/v2/posts')
  .then(response => response.json())
  .then(data => console.log(data.length))
  .catch(error => console.error('Error:', error));

What will be logged to the console if the site has 5 published posts?

Wordpress
fetch('https://example.com/wp-json/wp/v2/posts')
  .then(response => response.json())
  .then(data => console.log(data.length))
  .catch(error => console.error('Error:', error));
Aundefined
B5
CAn error message
D0
Attempts:
2 left
💡 Hint

Think about what the REST API returns and what data.length means.

📝 Syntax
intermediate
2:00remaining
Which option correctly fetches a single post by ID using async/await?

You want to fetch a post with ID 10 from the WordPress REST API using async/await syntax. Which code snippet is correct?

A
async function getPost() {
  const response = await fetch('https://example.com/wp-json/wp/v2/posts/10');
  const post = await response.json();
  return post;
}
B
async function getPost() {
  const response = fetch('https://example.com/wp-json/wp/v2/posts/10');
  const post = response.json();
  return post;
}
C
async function getPost() {
  const response = await fetch('https://example.com/wp-json/wp/v2/posts/10');
  const post = response.json();
  return post;
}
D
function getPost() {
  const response = await fetch('https://example.com/wp-json/wp/v2/posts/10');
  const post = await response.json();
  return post;
}
Attempts:
2 left
💡 Hint

Remember that await must be used with promises and inside async functions.

🔧 Debug
advanced
2:00remaining
Why does this fetch call fail with a CORS error?

This code tries to fetch posts from a WordPress REST API but fails with a CORS error in the browser console:

fetch('https://anotherdomain.com/wp-json/wp/v2/posts')
  .then(res => res.json())
  .then(data => console.log(data))
  .catch(err => console.error(err));

What is the most likely reason for the CORS error?

AThe fetch method should be POST instead of GET.
BThe fetch URL is incorrect and returns 404.
CThe server at anotherdomain.com does not allow cross-origin requests from your site.
DThe browser does not support fetch API.
Attempts:
2 left
💡 Hint

Think about browser security rules for cross-site requests.

state_output
advanced
2:00remaining
What is the final state of posts after this React useEffect fetch?

In a React component, this code fetches posts and sets state:

const [posts, setPosts] = React.useState([]);

React.useEffect(() => {
  fetch('https://example.com/wp-json/wp/v2/posts')
    .then(res => res.json())
    .then(data => setPosts(data))
    .catch(() => setPosts([]));
}, []);

If the fetch succeeds and returns 3 posts, what will posts.length be after the effect runs?

Wordpress
const [posts, setPosts] = React.useState([]);

React.useEffect(() => {
  fetch('https://example.com/wp-json/wp/v2/posts')
    .then(res => res.json())
    .then(data => setPosts(data))
    .catch(() => setPosts([]));
}, []);
A0
Bundefined
CAn error is thrown
D3
Attempts:
2 left
💡 Hint

Consider what happens when the fetch is successful and how state updates.

🧠 Conceptual
expert
2:00remaining
Which HTTP method and header combination is required to create a new post via WordPress REST API with JavaScript?

You want to create a new post on a WordPress site using the REST API and JavaScript. Which HTTP method and header must you include in your fetch request?

APOST method with 'Content-Type: application/json' and an Authorization header with a valid token
BGET method with 'Accept: application/json' header only
CPUT method with no headers
DDELETE method with 'Content-Type: text/plain' header
Attempts:
2 left
💡 Hint

Think about how to send data securely and the method used to create resources.