Last active
September 28, 2018 14:23
-
-
Save cubehrends/9418a1862f0c7513f4364360e68a29c9 to your computer and use it in GitHub Desktop.
WordPress GDPR Comments w/ Polylang via Child Theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function register_pll_strings() { | |
if( !function_exists( 'pll_register_string' ) ) return; // go away if polylang not active | |
pll_register_string('Policy Accept', 'I read your', 'CommentForm'); | |
pll_register_string('Privacy Policy', 'Privacy Policy', 'CommentForm'); | |
pll_register_string('Policy Page', 'privacy', 'CommentForm'); | |
pll_register_string('Error', 'ERROR', 'CommentForm'); | |
pll_register_string('Error Text', 'You must agree to our Privacy Policy.', 'CommentForm'); | |
} | |
add_action( 'init', 'register_pll_strings' ); | |
function custom_comment_consent_fields( $fields ) { | |
if( !function_exists( 'pll__' ) ) return; // go away if polylang not active | |
$site_url = get_site_url(); | |
$locale_simple = substr( get_locale(), 0, 2 ); | |
$locale_url = $site_url .'/'. $locale_simple .'/'; | |
$privacy_policy_consent_label = pll__( 'I read your' ) .' <a href="'. $locale_url . pll__( 'privacy' ) .'">'. pll__( 'Privacy Policy' ) .'</a>.'; | |
$fields['policy-consent'] = | |
'<p class="comment-form-policy-consent"> | |
<input id="wp-comment-policy-consent" name="wp-comment-policy-consent" value="yes" type="checkbox"'. ( isset( $_COOKIE['comment_author_privacy_' . COOKIEHASH] ) ? ' checked="checked"' : '' ) .' aria-required="true"> | |
<label for="wp-comment-policy-consent">'. $privacy_policy_consent_label .'</label> | |
<span class="comment-form-policy__required required">*</span> | |
</p>'; | |
return $fields; | |
} | |
add_filter('comment_form_default_fields', 'custom_comment_consent_fields'); | |
function verify_policy_consented( $commentdata ) { | |
if( !function_exists( 'pll__' ) ) return; // go away if polylang not active | |
if ( !isset( $_POST['wp-comment-policy-consent'] ) && !is_user_logged_in() ) { | |
$privacy_policy_consent_error = '<strong>'. pll__( 'ERROR' ) .':</strong> '. pll__( 'You must accept our Privacy Policy.' ); | |
wp_die( $privacy_policy_consent_error . '<p><a href="javascript:history.back()">' . __('« Back') . '</a></p>'); | |
} else { | |
$hashed_cookie = 'comment_author_privacy_' . COOKIEHASH; | |
if ( isset( $_POST['wp-comment-cookies-consent'] ) ) { | |
setcookie( $hashed_cookie, 1, time() + 3600 * 24 * 100, COOKIEPATH, COOKIE_DOMAIN, false ); | |
} else { | |
setcookie( $hashed_cookie, '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN ); | |
} | |
} | |
return $commentdata; | |
} | |
add_filter('preprocess_comment', 'verify_policy_consented'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment