华纳云香港服务器

WordPress 发表评论只需输入评论者名称

WordPress主题推荐

默认WordPress 发表评论只能同时设置”必须填入姓名和电子邮件地址“,如果认为输入电子邮件会影响评论者的感受,可以通过修改程序文件,让评论者只需要输入姓名即可。

以WordPress 4.6为例,打开WP程序wp-includes目录的comment.php文件,在大约第2886行找到:

  1. if ( get_option( ‘require_name_email’ ) && ! $user->exists() ) {
  2.     if ( 6 > strlen$comment_author_email ) ||  == $comment_author ) {
  3.         return new WP_Error( ‘require_name_email’, __( ‘<strong>ERROR</strong>: please fill the required fields (name, email).’ ), 200 );
  4.     } elseif ( ! is_email( $comment_author_email ) ) {
  5.         return new WP_Error( ‘require_valid_email’, __( ‘<strong>ERROR</strong>: please enter a valid email address.’ ), 200 );
  6.     }
  7. }

替换为:

  1. if ( get_option( ‘require_name_email’ ) && ! $user->exists() ) {
  2.     if (  == $comment_author )
  3.         return new WP_Error( ‘require_name_email’, __( ‘<strong>错误</strong>:请填写您的名称!’ ), 200 );
  4. }
提示

建议使用专门的文本编辑工具,比如Notepad++(免费)修改程序文件,点击Notepad++编辑器菜单中的“格式”,选择以UTF-8 无BOM格式编码,否则其中的中文汉字提示会显示乱码。

进入WP后台→设置→讨论→其他评论设置中勾选”评论作者必须填入姓名和电子邮件地址“,完成上面的修改后,再次发表评论时只需要填写姓名一项就可以了。

但只到这步还是不太完美,因为评论表单中还有”电子邮件“和”站点“项,会让人产生误会,所以,还需要将这俩表单去掉,修改主题模板文件比较麻烦,直接用CSS隐藏。

以WordPress默认主题Twenty Sixteen为例,查看网页原代码,发现”电子邮件“和”站点“两项的DIV选择器是.comment-form-email和.comment-form-url,将下面的样式代码添加到主题样式文件style.css的最后:

  1. .comment-form-email, .comment-form-url{
  2.     displaynone;
  3. }

添加上面代码后,”电子邮件“和”站点“两个表单被隐藏,只剩下名称一项。

如果你的主题使用了Ajax comments评论提交代码,修改方法相似,找到当前主题目录中的comment-ajax.php模板文件,找到类似代码:

  1. if ( get_option(‘require_name_email’) && !$user->ID ) {
  2.     if ( 6 > strlen($comment_author_email) ||  == $comment_author )
  3.         err(‘<i class=“fa fa-exclamation-circle”></i>提示:必须填写昵称及邮件。’); // 将 wp_die 改为错误提示
  4.     elseif ( !is_email($comment_author_email))
  5.         err(‘<i class=“fa fa-exclamation-circle”></i>提示:请输入一个有效的电子邮件地址。’);// 将 wp_die 改为错误提示
  6. }

同样用上面的代码替换即可。

赞 (0) 打赏
版权声明:本文为转载文章,来源于 知更鸟 ,版权归原作者所有!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
wu