Using WordPress ‘is_client_error()’ PHP function

The is_client_error() WordPress PHP function checks if an HTTP status code is a client error.

Usage

$is_client_error = is_client_error( $status_code );

Example:

$is_client_error = is_client_error(404);

Parameters

  • $status_code (int) – The HTTP status code to check.

More information

See WordPress Developer Resources: is_client_error()

Examples

Check if a status code is a client error

This example checks if the given status code is a client error.

$status_code = 404;
if (is_client_error($status_code)) {
    echo "This is a client error!";
} else {
    echo "This is not a client error.";
}

Check multiple status codes

This example checks multiple status codes and outputs the result.

$status_codes = array(200, 404, 500);

foreach ($status_codes as $code) {
    if (is_client_error($code)) {
        echo "Status code {$code} is a client error.<br>";
    } else {
        echo "Status code {$code} is not a client error.<br>";
    }
}

Validate user input

This example validates user input as an HTTP status code and checks if it’s a client error.

$user_input = "404";

if (is_numeric($user_input) && is_client_error((int)$user_input)) {
    echo "The input is a valid client error status code.";
} else {
    echo "The input is not a valid client error status code.";
}

Handle a client error based on the status code

This example handles a client error by displaying a custom message based on the status code.

$status_code = 404;

if (is_client_error($status_code)) {
    switch ($status_code) {
        case 404:
            echo "Oops! The requested page was not found.";
            break;
        case 403:
            echo "Sorry! You do not have permission to access this page.";
            break;
        default:
            echo "An error occurred. Please try again later.";
    }
}

Check if a status code is NOT a client error

This example checks if the given status code is not a client error.

$status_code = 200;
if (!is_client_error($status_code)) {
    echo "This is not a client error!";
} else {
    echo "This is a client error.";
}