WordPress评论Cookie功能

474次阅读
没有评论

共计 1802 个字符,预计需要花费 5 分钟才能阅读完成。

自升级 WordPress4.9.6 后,新增记录评论 Cookie 功能,方便评论者下次发表评论无需再次输入名称和邮箱地址。

主题默认使用了 comment_form() 函数,并在设置 --- 讨论 --- 勾选了“Show comments cookies opt-in checkbox.”会在评论模块中显示一个记录评论 Cookie 的复选框。

默认评论模块复选框的提示文字“Save my name, email, and website in this browser for the next time I comment.”可能并符合你的习惯。我们可以通过下面的代码自定义这段文字。

将下面代码添加到当前主题 functions.php 中:

默认自动勾选:

function comment_form_change_cookies_consent($fields) {$commenter = wp_get_current_commenter();
    $consent   = emptyempty($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’);

默认不勾选

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’);

如果你的主题使用了自定义评论函数,评论模块中没出现记录评论 Cookie 的复选框,可以使用下面的代码添加该功能,默认记录 Cookie 并隐藏难看的复选框:

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);
}

如你的主题使用了的 AJAX 提交评论,已默认记录 Cookie,无需上面的操作。

想完全禁用这个功能,可以用下面的代码:

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

或者

add_filter(‘comment_form_field_cookies’,’__return_false’);

转至 http://zmingcx.com/custom-wordpress-comment-on-cookies.html

正文完
 0
水东柳
版权声明:本站原创文章,由 水东柳 2019-04-10发表,共计1802字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)