Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.