Using WordPress ‘is_server_error()’ PHP function

The is_server_error() WordPress PHP function checks if the current response is a server error (HTTP status code 500).

Usage

$is_error = is_server_error();

Parameters

This function does not have any parameters.

More information

See WordPress Developer Resources: is_server_error()

Examples

Display a custom message for server errors

if (is_server_error()) {
    echo "We are currently experiencing technical difficulties. Please try again later.";
}

Redirect users to a custom error page

if (is_server_error()) {
    wp_redirect('/custom-server-error-page/');
    exit;
}

Log server errors for debugging

if (is_server_error()) {
    error_log('A server error occurred on ' . get_the_permalink());
}

Send an email to the admin when a server error occurs

if (is_server_error()) {
    $to = get_bloginfo('admin_email');
    $subject = 'Server Error on ' . get_bloginfo('name');
    $message = 'A server error occurred on ' . get_the_permalink();
    wp_mail($to, $subject, $message);
}

Load a custom template for server errors

if (is_server_error()) {
    get_template_part('template-parts/content', 'server-error');
}