Halloween Sale 15% off Ends in See the plans
PHP

Site Health Checks for Privacy Settings

Adds Site Health tests that check whether a privacy policy page is published, whether full commenter IP addresses are being stored, and whether the comment cookie consent checkbox is turned on.

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

Code

add_filter( 'site_status_tests', function ( $tests ) {
    $result = function ( $id, $ok, $good, $bad, $fix ) {
        return array(
            'label'       => $ok ? $good : $bad,
            'status'      => $ok ? 'good' : 'recommended',
            'badge'       => array( 'label' => 'Privacy', 'color' => $ok ? 'blue' : 'orange' ),
            'description' => '<p>' . esc_html( $ok ? $good . '.' : $fix ) . '</p>',
            'test'        => $id,
        );
    };

    $tests['direct']['wpide_privacy_page'] = array(
        'label' => 'Privacy policy page',
        'test'  => function () use ( $result ) {
            $id = (int) get_option( 'wp_page_for_privacy_policy' );
            return $result( 'wpide_privacy_page', $id && 'publish' === get_post_status( $id ), 'A privacy policy page is published', 'No privacy policy page is published', 'Create and publish a privacy policy page, then select it under Settings > Privacy.' );
        },
    );

    $tests['direct']['wpide_comment_ips'] = array(
        'label' => 'Commenter IP addresses',
        'test'  => function () use ( $result ) {
            global $wpdb;
            $ip = (string) $wpdb->get_var( "SELECT comment_author_IP FROM {$wpdb->comments} WHERE comment_author_IP <> '' ORDER BY comment_ID DESC LIMIT 1" );
            $ok = '' === $ip || wp_privacy_anonymize_ip( $ip ) === $ip;
            return $result( 'wpide_comment_ips', $ok, 'New comments do not store full IP addresses', 'Full IP addresses are stored with comments', 'Store shortened IP addresses for comments, or none at all, unless your spam protection needs them.' );
        },
    );

    $tests['direct']['wpide_comment_cookies'] = array(
        'label' => 'Comment cookie consent',
        'test'  => function () use ( $result ) {
            $ok = ! get_option( 'default_comment_status' ) || 'closed' === get_option( 'default_comment_status' ) || get_option( 'show_comments_cookies_opt_in' );
            return $result( 'wpide_comment_cookies', (bool) $ok, 'Commenters are asked before their details are saved in a cookie', 'Comment cookies are set without asking', 'Turn on "Show comments cookies opt-in checkbox" under Settings > Discussion.' );
        },
    );

    return $tests;
} );

Notes

Each result explains how to fix the setting; pair it with the IP shortening and consent snippets.

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.