Skip to content

Instantly share code, notes, and snippets.

@thelovekesh
Created March 20, 2022 09:27
Show Gist options
  • Save thelovekesh/688c6cffc1668e618c4b4c53039b3dcc to your computer and use it in GitHub Desktop.
Save thelovekesh/688c6cffc1668e618c4b4c53039b3dcc to your computer and use it in GitHub Desktop.
Add site health tests to your WordPress site health screen
<?php
/**
* Plugin Name: Demo Site Health Test
* Description: Add site health tests to the WordPress site-health dashboard.
* Version: 1.0.0
* Requires at least: 5.1
* Requires PHP: 5.6
* Author: thelovekesh
* Author URI: https://thelovekesh.com/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Update URI: https://example.com/my-plugin/
* Text Domain: lps
*
* @package thelovekesh/site-health-tests
*/
// Add site health tests in WordPress site-health dashboard.
add_filter(
'site_status_tests',
'thelovekesh_demo_site_health_test'
);
/**
* Add site health tests.
*
* @param array $tests WordPress site-health tests.
* @return array Tests
*/
function thelovekesh_demo_site_health_test( $tests ) {
// @see <https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/class-wp-site-health.php#L2429>.
$tests['direct']['wp_demo_site_health_test'] = array(
'label' => __( 'Demo Site Health Test', 'lps' ),
'test' => 'thelovekesh_demo_site_health_test_callback',
);
return $tests;
}
/**
* Add site health tests callback.
*
* @return array Tests
*/
function thelovekesh_demo_site_health_test_callback() {
// @see <https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/class-wp-site-health.php#L170>.
$results = array(
'label' => __( 'Demo Site Health Test', 'lps' ),
'status' => 'good',
'badge' => array(
'label' => __( 'Demo Site Health Test', 'lps' ),
'color' => 'green',
),
'description' => __( 'Add site health tests to the WordPress site-health dashboard.', 'lps' ),
'actions' => '',
'test' => 'wp_demo_site_health_test',
);
/*
* You can evaluate the site health test results and change the status, badge, description, actions, etc.
* And manipulate the site health test results array.
*/
// Change status to 'critical'.
$results['status'] = 'critical';
// Change badge color.
$results['badge']['color'] = 'red';
// Change description.
$results['description'] = __( 'You have some critical issues with your site.', 'lps' );
// Provide action where the critical issues can be fixed.
$results['actions'] = sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'options-general.php' ) ),
__( 'Fix critical issues', 'lps' )
);
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment