Have a look at what point in your code you’re making calls to the WordPress REST API or any other code which makes use of WP_Site_Health
.
If you’re making calls in your <theme>/functions.php
file, for example, it won’t work because functions.php
is included before class WP_Site_Health
in wp-settings.php
.
See wp-settings.php
code (WordPress 5.7.2):
// Load the functions for the active theme, for both parent and child theme if applicable.
foreach ( wp_get_active_and_valid_themes() as $theme ) {
if ( file_exists( $theme . '/functions.php' ) ) {
include $theme . '/functions.php';
}
}
unset( $theme );
/**
* Fires after the theme is loaded.
*
* @since 3.0.0
*/
do_action( 'after_setup_theme' );
// Create an instance of WP_Site_Health so that Cron events may fire.
if ( ! class_exists( 'WP_Site_Health' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
}
WP_Site_Health::get_instance();
Despite the lack of elegance, one thing you can do about it is to ensure the class is loaded by copying part of the code from wp-settings.php
and pasting it before your code. For example, in your <theme>/functions.php
, you can do this:
if ( ! class_exists( 'WP_Site_Health' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
}
WP_Site_Health::get_instance();
---8<---
code depending on WP_Site_Health
---8<---