REST API & Developer PHP Intermediate

Add Your Own Site Health Check

Add a test to Tools > Site Health that warns when plugins your site depends on, like backups or security, are not active.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 39 lines
add_filter( 'site_status_tests', function ( array $tests ): array {
	$tests['direct']['nahnu_required_plugins'] = [
		'label' => 'Required plugins',
		'test'  => 'nahnu_site_health_required_plugins',
	];
	return $tests;
} );

function nahnu_site_health_required_plugins(): array {
	// Plugin file => name to show.
	$required = [
		'updraftplus/updraftplus.php' => 'UpdraftPlus (backups)',
	];

	if ( ! function_exists( 'is_plugin_active' ) ) {
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
	}

	$missing = array_filter( $required, fn ( string $name, string $file ): bool => ! is_plugin_active( $file ), ARRAY_FILTER_USE_BOTH );

	$result = [
		'label'       => 'All required plugins are active',
		'status'      => 'good',
		'badge'       => [ 'label' => 'Site setup', 'color' => 'blue' ],
		'description' => '<p>Every plugin this site relies on is installed and active.</p>',
		'actions'     => '',
		'test'        => 'nahnu_required_plugins',
	];

	if ( $missing ) {
		$result['label']       = 'Some required plugins are not active';
		$result['status']      = 'critical';
		$result['badge']       = [ 'label' => 'Site setup', 'color' => 'red' ];
		$result['description'] = '<p>These plugins should be active: ' . esc_html( implode( ', ', $missing ) ) . '.</p>';
		$result['actions']     = '<p><a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">Go to Plugins</a></p>';
	}

	return $result;
}

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Site Health checks the things WordPress cares about, but not the things your site needs, like a backup plugin being active. Adding your own test puts those checks in the same place, and failures show up in the Site Health dashboard widget.

How it works

The site_status_tests filter registers tests. A “direct” test runs when the Site Health screen loads and calls your function, which returns a result with a label, a status of good, recommended or critical, a badge and a description.

This example checks a list of plugin files with is_plugin_active() and reports any that aren’t active as critical.

Things to watch

  • Change the $required list to the plugin files you rely on. You’ll find them as folder/main-file.php in wp-content/plugins.
  • For slow checks, use an 'async' test so Site Health doesn’t hang.

How to test it

Deactivate one of the listed plugins and open Tools → Site Health. “Some required plugins are not active” should appear as a critical issue.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only