Add your own checks to the Site Health screen
Tools → Site Health, added in WordPress 5.2, checks the PHP version, HTTPS, scheduled events, the REST API and more, and shows the results as good, recommended or critical. Plugins can add their own tests with the site_status_tests filter. That is the right place to tell administrators about a missing PHP extension or an unconfigured API key, instead of a notice on every admin screen or a feature that fails silently.
Problem: A plugin needs the PHP intl extension and an API key. When either is missing, its features break without any message, and the site owner learns about it from a customer.
Solution: Register “direct” tests with the site_status_tests filter. Each test is a function that returns an array with a label, a status (good, recommended or critical), a badge, a description, optional actions and the test ID.
Registering the tests
<?php
add_filter( 'site_status_tests', function ( $tests ) {
$tests['direct']['ha_intl_extension'] = array(
'label' => __( 'My Plugin: PHP intl extension', 'textdomain' ),
'test' => 'ha_site_health_intl',
);
$tests['direct']['ha_api_key'] = array(
'label' => __( 'My Plugin: API key', 'textdomain' ),
'test' => 'ha_site_health_api_key',
);
return $tests;
} );
Direct tests run while the Site Health page is generated, so they must be fast: no remote requests here.
The test functions
<?php
function ha_site_health_intl() {
$result = array(
'label' => __( 'The PHP intl extension is installed', 'textdomain' ),
'status' => 'good',
'badge' => array( 'label' => __( 'My Plugin', 'textdomain' ), 'color' => 'blue' ),
'description' => '<p>' . __( 'My Plugin uses intl to format prices and dates.', 'textdomain' ) . '</p>',
'actions' => '',
'test' => 'ha_intl_extension',
);
if ( ! extension_loaded( 'intl' ) ) {
$result['label'] = __( 'The PHP intl extension is missing', 'textdomain' );
$result['status'] = 'critical';
$result['badge']['color'] = 'red';
$result['description'] = '<p>' . __( 'Without intl, prices and dates are shown unformatted. Ask your host to enable the extension.', 'textdomain' ) . '</p>';
}
return $result;
}
function ha_site_health_api_key() {
$configured = (bool) get_option( 'ha_api_key' );
return array(
'label' => $configured ? __( 'The API key is set', 'textdomain' ) : __( 'The API key is not set', 'textdomain' ),
'status' => $configured ? 'good' : 'recommended',
'badge' => array( 'label' => __( 'My Plugin', 'textdomain' ), 'color' => $configured ? 'blue' : 'orange' ),
'description' => '<p>' . __( 'The API key is needed for automatic stock updates.', 'textdomain' ) . '</p>',
'actions' => $configured ? '' : sprintf(
'<p><a href="%s">%s</a></p>',
esc_url( admin_url( 'options-general.php?page=ha-settings' ) ),
esc_html__( 'Enter the API key', 'textdomain' )
),
'test' => 'ha_api_key',
);
}
Use critical only when something is broken, recommended for improvements. Critical results count towards the “should be improved” summary on the Site Health page and the dashboard widget, so too many of them teach administrators to ignore the screen.
Slow checks
A check that needs a remote request (is the external API reachable?) should not run as a direct test. Store its result in a transient from a cron job and let the direct test only read the transient; the page then loads fast and the result is at most a few hours old.
Update (2020): WordPress 5.6 made asynchronous tests easier: an async test can point to a REST route ('has_rest' => true) and give an async_direct_test callback that is used when the result is needed without the browser. On 5.2–5.5 async tests are run through admin-ajax.php instead.
NOTE: Site Health also has an “Info” tab that site owners copy into support requests. Add your plugin’s settings there with the debug_information filter; it saves a lot of back and forth when somebody asks for help, but leave out secrets like API keys.