Discover how WordPress takes the headache out of talking to your website behind the scenes!
Why Request and response handling in Wordpress? - Purpose & Use Cases
Imagine building a website where every time a user clicks a button, you have to write separate code to check the request, process data, and send back the right response manually.
Doing this by hand is slow and confusing. You might forget to check important details or send the wrong response, making your site buggy and hard to fix.
WordPress provides built-in ways to handle requests and responses smoothly, so you focus on what your site should do, not on the messy details of communication.
if ($_SERVER['REQUEST_METHOD'] === 'POST') { $data = $_POST['name']; echo 'Hello ' . $data; }
add_action('rest_api_init', function() { register_rest_route('myplugin/v1', '/hello', array('methods' => 'POST', 'callback' => 'my_hello_callback')); }); function my_hello_callback($request) { $name = $request->get_param('name'); return 'Hello ' . $name; }
This lets you build interactive, reliable websites that talk to users and other systems easily and safely.
When a visitor submits a contact form, WordPress handles the request, processes the data, and sends back a confirmation message without you writing complex code.
Manual request handling is error-prone and slow.
WordPress simplifies request and response management.
This makes your site more reliable and easier to build.
Practice
Solution
Step 1: Understand HTTP POST data handling
Data sent by users through a POST request is stored in the$_POSTsuperglobal in PHP.Step 2: Differentiate from other superglobals
$_GETholds URL query parameters,$_REQUESTcombines GET, POST, and COOKIE but is less specific, and$GLOBALSholds all global variables.Final Answer:
$_POST-> Option BQuick Check:
POST data =$_POST[OK]
- Confusing $_GET with $_POST
- Using $_REQUEST without knowing its mixed content
- Assuming $GLOBALS holds request data
Solution
Step 1: Understand WordPress REST API callback requirements
REST API callbacks should return aWP_REST_Response, array, orWP_Error. The framework handles headers, JSON encoding, and status.Step 2: Compare other options
wp_send_json($data);sends JSON and exits withwp_die(), suitable for AJAX but bypasses REST processing/filters.echo json_encode($data);outputs JSON without headers or exit.header('Content-Type: text/plain'); echo $data;wrong type.Final Answer:
return new WP_REST_Response($data, 200); -> Option DQuick Check:
Return WP_REST_Response [OK]
- Using wp_send_json() (intended for AJAX handlers)
- Using echo without proper headers and exit
- Sending wrong content type header
function get_custom_data(\WP_REST_Request $request) {
$param = $request->get_param('id');
if ($param === '42') {
return ['message' => 'Found'];
}
return new WP_Error('not_found', 'ID not found', ['status' => 404]);
}
What will be the HTTP status code if the request URL includes ?id=42?Solution
Step 1: Analyze the parameter check
The function checks ifidequals '42'. If yes, it returns an array with message 'Found'.Step 2: Understand default REST API response codes
Returning an array results in a 200 OK status. Returning WP_Error sets the error status (404 here).Final Answer:
200 -> Option AQuick Check:
Valid data returns 200 OK [OK]
- Assuming WP_Error always returns 200
- Confusing string '42' with integer 42
- Ignoring default REST API status codes
function my_callback(\WP_REST_Request $request) {
$data = $request->get_param('data');
wp_send_json_success($data);
echo 'Done';
}
What is the problem with this code?Solution
Step 1: Understand wp_send_json_success behavior
This function sends JSON response and calls exit(), stopping further code execution.Step 2: Analyze code after wp_send_json_success
Theecho 'Done';line is unreachable because the script exits before it.Final Answer:
The echo statement after wp_send_json_success() will never run -> Option AQuick Check:
wp_send_json_success() exits script [OK]
- Expecting code after wp_send_json_success() to run
- Confusing get_param() usage
- Thinking wp_send_json_success() returns data instead of exiting
Solution
Step 1: Retrieve JSON body parameters properly
In WordPress REST API,$request->get_json_params()safely parses JSON body data.Step 2: Return a proper REST response object
ReturningWP_REST_Responsewith data and status code is the recommended way to send structured responses.Step 3: Evaluate other options
A usesget_body_params()which checks form data ($_POST), empty for JSON body POSTs. C uses$_POST['name']directly, same issue. D manually reads input bypassing framework helpers and useswp_send_json()which exits.Final Answer:
Use $request->get_json_params()['name'] and return WP_REST_Response -> Option CQuick Check:
Use get_json_params() + WP_REST_Response for JSON POST [OK]
- Using $_POST or get_body_params for JSON body data
- Not decoding JSON body properly
- Returning raw arrays instead of WP_REST_Response
Ready to prove it?
Pass the test and this pattern moves unknown
