Site Health Check for Security Headers
Adds a Site Health test that loads your homepage and reports which common security headers (HSTS, CSP, nosniff, framing, referrer and permissions policies) are missing.
Opens WPIDE on
Code
add_filter( 'site_status_tests', function ( $tests ) {
$tests['direct']['wpide_security_headers'] = array(
'label' => 'Security headers',
'test' => function () {
$headers = get_transient( 'wpide_sh_headers' );
if ( false === $headers ) {
$response = wp_remote_get( home_url( '/' ), array( 'timeout' => 8, 'redirection' => 2, 'sslverify' => apply_filters( 'https_local_ssl_verify', false ) ) );
$headers = is_wp_error( $response ) ? array() : array_change_key_case( wp_remote_retrieve_headers( $response )->getAll(), CASE_LOWER );
set_transient( 'wpide_sh_headers', $headers, 12 * HOUR_IN_SECONDS );
}
$wanted = array(
'x-content-type-options' => 'X-Content-Type-Options',
'referrer-policy' => 'Referrer-Policy',
'permissions-policy' => 'Permissions-Policy',
'content-security-policy' => 'Content-Security-Policy',
);
if ( is_ssl() || 0 === strpos( home_url(), 'https:' ) ) {
$wanted['strict-transport-security'] = 'Strict-Transport-Security';
}
$missing = array();
foreach ( $wanted as $key => $label ) {
if ( empty( $headers[ $key ] ) && ! ( 'content-security-policy' === $key && ! empty( $headers['content-security-policy-report-only'] ) ) ) {
$missing[] = $label;
}
}
$csp = isset( $headers['content-security-policy'] ) ? implode( ' ', (array) $headers['content-security-policy'] ) : '';
if ( empty( $headers['x-frame-options'] ) && false === strpos( $csp, 'frame-ancestors' ) ) {
$missing[] = 'X-Frame-Options (or CSP frame-ancestors)';
}
return array(
'label' => $missing ? 'Some security headers are missing' : 'Security headers are in place',
'status' => $missing ? 'recommended' : 'good',
'badge' => array( 'label' => 'Security', 'color' => 'blue' ),
'description' => $missing
? '<p>' . esc_html( 'Your homepage does not send: ' . implode( ', ', $missing ) . '.' ) . '</p>'
: '<p>Your homepage sends the common security headers.</p>',
'test' => 'wpide_security_headers',
);
},
);
return $tests;
} );
Notes
The result is cached for 12 hours; headers added by your server or CDN are detected too.
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.