Uptime Check Endpoint That Tests the Database
Adds a lightweight /wp-json/site/v1/ping endpoint for uptime monitors. It answers 200 only when WordPress can reach the database, and 503 otherwise, without exposing any versions.
Opens WPIDE on
Code
add_action( 'rest_api_init', function () {
register_rest_route( 'site/v1', '/ping', array(
'methods' => WP_REST_Server::READABLE,
'permission_callback' => '__return_true',
'callback' => function () {
global $wpdb;
$db_ok = '1' === (string) $wpdb->get_var( 'SELECT 1' );
$response = new WP_REST_Response( array(
'status' => $db_ok ? 'ok' : 'database_error',
'time' => time(),
), $db_ok ? 200 : 503 );
$response->header( 'Cache-Control', 'no-store, max-age=0' );
return $response;
},
) );
} );
Notes
Point your monitor at https://example.com/wp-json/site/v1/ping and alert on any status other than 200.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.