wordpress增加评论自定义字段并用QQ邮箱接收消息

  • 2021-01-04 20:56:18
  • 1,624 次阅读
  • 稿源:天马行空

wordpress网站经常有网友评论,往往不能及时看到消息。很多时候,我们每天使用QQ的次数比较多,如果当有人留言时,能够用QQ邮箱接收评论内容,我们可能会及时收到QQ邮件看到消息。但有时,wordpress评论表单只提供四个默认的字段,分别为昵称、邮箱、网址和评论内容,只能满足一般的博客网站需求,如果想增加评论字段该怎么做?那么我们就具体看一看。

wordpress增加评论自定义字段并用QQ邮箱接收消息

一、添加评论自定义手机字段

add_filter('comment_form_default_fields','comment_form_add_tel');
function comment_form_add_tel($fields) {
  $label = __( '手机' ); 
  $value = isset($_POST['tel']) ? $_POST['tel'] : false;
 
  $fields['tel'] =<<<HTML
<p class="comment-form-tel">
<label for="tel">{$label}</label>
<input id="tel" name="tel" type="text" value="{$value}" size="30" />
</p>
HTML;
  return $fields;
}

二、添加评论自定义手机字段写入数据库

add_action('wp_insert_comment','wp_insert_tel',10,2);
function wp_insert_tel($comment_ID,$commmentdata) {
  $tel = isset($_POST['tel']) ? $_POST['tel'] : false;
  update_comment_meta($comment_ID,'_tel',$tel);
}

三、后台管理页面显示自定义手机字段

add_filter( 'manage_edit-comments_columns', 'my_comments_columns' );
add_action( 'manage_comments_custom_column', 'output_my_comments_columns', 10, 2 );
function my_comments_columns( $columns ){
    $columns[ '_tel' ] = __( '手机' );        
    return $columns;
}
function  output_my_comments_columns( $column_name, $comment_id ){
    switch( $column_name ){
    case '_tel';
    echo get_comment_meta( $comment_id, '_tel', true );
    break;
}}

四、前端添加自定义手机字段

<p class="comment-form-author">
<label for="author">昵称<span class="required">(必填)</span></label>
<input placeholder="昵称" name="author" id="author" value="<?php echo $comment_author; ?>" type="text" size="30">
</p>
<p class="comment-form-email">
<label for="email">邮箱<span class="required">(必填)</span></label>
<input placeholder="邮箱" name="email" id="email" value="<?php echo $comment_author_email; ?>" type="email" size="30">
</p>
<p class="comment-form-url">
<label for="url">网址</label>
<input placeholder="网址" id="url" name="url" value="<?php echo $comment_author_url; ?>" type="url" size="30">
</p>			
<p class="comment-form-tel">
<label for="tel">手机</label>
<input placeholder="手机" id="tel" name="url" value="<?php echo $tel; ?>" type="tel" size="30">
</p>

五、SMTP邮箱设置

add_action('phpmailer_init', 'mail_smtp');
function mail_smtp( $phpmailer ) {
$phpmailer->FromName = 'maxing128.com'; //发件人名称
$phpmailer->Host = 'smtp.qq.com'; //修改为你使用的邮箱SMTP服务器
$phpmailer->Port = 465; //SMTP端口
$phpmailer->Username = '1696128890@qq.com'; //邮箱账户
$phpmailer->Password = '*********'; //邮箱密码(此处填写QQ邮箱生成的授权码)
$phpmailer->From = '1696128890@qq.com'; //邮件显示邮箱,这个可以与发信邮箱不同
$phpmailer->SMTPAuth = true;
$phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25时->留空,465时->ssl)
$phpmailer->IsSMTP();
}

六、QQ邮箱配置

1.登录QQ邮箱,打开设置->账户,往下拉,注意红色框内的信息。

2.开通POP3/SMTP,点击开启按钮后,弹出下面的对话框,单击确定,会提示发送短信验证,如下图

3.配置邮件客户端短信验证

4.开通IMAP/SMTP同样也会发送短信验证

5.生成授权码,如下图:

以上准备好之后,wordpress评论邮件通知测试,如下图

喜欢 4

文章评论 (0)

表情

大眼 可爱 大笑 坏笑 害羞 发怒 折磨 快哭了 大哭 白眼 晕 流汗 困 腼腆 惊讶 憨笑 色 得意 骷髅 囧 睡觉 眨眼 亲亲 疑问 闭嘴 难过 淡定 抗议 鄙视 猪头