0
0
Wordpressframework~10 mins

Request and response handling in Wordpress - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get a query parameter named 'id' from the URL in WordPress.

Wordpress
$id = get_query_var('[1]');
Drag options to blanks, or click blank then click option'
Apage
Bpost_id
Cid
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong parameter name like 'post_id' instead of 'id'.
Forgetting to put the parameter name as a string.
2fill in blank
medium

Complete the code to send a JSON response with status 200 in WordPress.

Wordpress
wp_send_json_success([1]);
Drag options to blanks, or click blank then click option'
A'Success'
Barray('message' => 'Success')
Ctrue
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of an array.
Passing null which sends empty data.
3fill in blank
hard

Fix the error in the code to check if a POST request contains a 'name' parameter.

Wordpress
if (isset($_POST['[1]'])) {
    $name = sanitize_text_field($_POST['name']);
}
Drag options to blanks, or click blank then click option'
Aname
Busername
Cuser
Dfullname
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for a different key than the one used to read the value.
Not sanitizing the input after checking.
4fill in blank
hard

Fill both blanks to create a REST API endpoint callback that returns a JSON response with a message.

Wordpress
function my_api_callback() {
    return wp_send_json_[1](array('message' => '[2]'));
}
Drag options to blanks, or click blank then click option'
Asuccess
Berror
CHello World
DFailed
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'error' instead of 'success' for a positive response.
Putting an error message instead of a greeting.
5fill in blank
hard

Fill all three blanks to register a REST API route that accepts GET requests and uses a callback.

Wordpress
add_action('rest_api_init', function () {
    register_rest_route('myplugin/v1', '/data', array(
        'methods' => [1],
        'callback' => [2],
        'permission_callback' => [3]
    ));
});
Drag options to blanks, or click blank then click option'
A'POST'
B'GET'
C'__return_true'
Dmy_api_callback
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'POST' instead of 'GET' for the method.
Not using a valid permission callback function.