The is_error() WordPress PHP function determines if an HTTP response code is an error.
Usage
To use the is_error() function, simply pass the HTTP response code as an argument. The function will return true
if it’s an error, or false
if it’s not.
$is_error = is_error($response_code);
Parameters
$sc
(integer): The HTTP response code to check for an error.
More information
See WordPress Developer Resources: is_error()
Examples
Checking if a response code is an error
This example demonstrates how to use the is_error() function to check if a response code is an error.
$response_code = 404; $is_error = is_error($response_code); echo $is_error ? 'Error' : 'Not an error'; // Output: Error
Handling different response codes
This example shows how to handle different response codes using the is_error() function.
$response_code = 200; if (is_error($response_code)) { echo 'Error'; } else { echo 'Success'; } // Output: Success
Checking multiple response codes
In this example, we use the is_error() function to check multiple response codes for errors.
$response_codes = array(200, 201, 400, 500); foreach ($response_codes as $code) { echo "Code $code: " . (is_error($code) ? 'Error' : 'Not an error') . "\n"; }
Handling server response
This example shows how to handle a server response using the is_error() function.
$response = wp_remote_get('https://example.com'); $response_code = wp_remote_retrieve_response_code($response); if (is_error($response_code)) { echo 'Error'; } else { echo 'Success'; }
Custom error messages
This example demonstrates using custom error messages with the is_error() function.
$response_code = 403; if (is_error($response_code)) { echo "Error: $response_code"; } else { echo 'Success'; } // Output: Error: 403