栖息邦

自定义WordPress评论Cookie功能

https://www.xintheme.com/wpjiaocheng/120166.html

function comment_form_change_cookies_consent( $fields ) {
$commenter = wp_get_current_commenter();
$consent = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';
$fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"' . $consent . ' />' .
'<label for="wp-comment-cookies-consent">记住我的信息</label></p>';
return $fields;
}
add_filter( 'comment_form_default_fields', 'comment_form_change_cookies_consent' );

默认不勾选文章源自知更鸟-https://zmingcx.com

/custom-wordpress-comment-on-cookies.html

function comment_form_not_checked_cookies_consent( $fields ) {$fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes" />' .'<label for="wp-comment-cookies-consent">记住我的信息</label></p>';
return $fields;
}
add_filter( 'comment_form_default_fields', 'comment_form_not_checked_cookies_consent' );

源代码出自:https://wordpress.org/support/topic/update-4-9-6-checkbox-comments-privacy-approved/文章源自知更鸟-https://zmingcx.com/custom-wordpress-comment-on-cookies.html

如果你的主题使用了自定义评论函数,评论模块中没出现记录评论Cookie的复选框,可以使用下面的代码添加该功能,默认记录Cookie并隐藏难看的复选框:文章源自知更鸟-https://zmingcx.com/custom-wordpress-comment-on-cookies.html


add_action('set_comment_cookies','coffin_set_cookies',10,3);
function coffin_set_cookies( $comment, $user, $cookies_consent){
$cookies_consent = true;
wp_set_comment_cookies($comment, $user, $cookies_consent);
}

源代码出自:https://fatesinger.com/100240文章源自知更鸟-https://zmingcx.com/custom-wordpress-comment-on-cookies.html

如你的主题使用了的AJAX 提交评论,已默认记录Cookie, 无需上面的操作。文章源自知更鸟-https://zmingcx.com/custom-wordpress-comment-on-cookies.html

想完全禁用这个功能,可以用下面的代码:文章源自知更鸟-https://zmingcx.com/custom-wordpress-comment-on-cookies.html

function comment_form_hide_cookies_consent( $fields ) {unset( $fields['cookies'] );return $fields;}
add_filter( 'comment_form_default_fields', 'comment_form_hide_cookies_consent' );

或者文章源自知更鸟-https://zmingcx.com/custom-wordpress-comment-on-cookies.html