wordpress创建自定义后台评论,并且发送邮件

由于客户需要定义一个举报系统,所以我就使用了wordpress自带的评论系统进行修改。是后台能够显示用户想要的字段。后台显示效果如下:

wordpress创建自定义后台评论,并且发送邮件_第1张图片

含有填写人的姓名,内容(评论),电话,身份证,地址。当然这些都用js检验。

前台的页面如下所示:

wordpress创建自定义后台评论,并且发送邮件_第2张图片

将表单填写完成后,进行提交。然后就能在后台进行显示。在页面处引用评论的模板。


然后在functions.php中引用自定义的后台字段:

require_once( TEMPLATEPATH . '/inc/comment_custom.php' );

然后在相应的文件夹下面进行comment_custom.php的编写:




HTML; return $fields; } add_filter('comment_form_default_fields','comment_form_add_sfz'); function comment_form_add_sfz($fields) { $label = __( '身份证' ); $value = isset($_POST['sfz']) ? $_POST['sfz'] : false; $fields['sfz'] =<<

HTML; return $fields; } add_filter('comment_form_default_fields','comment_form_add_dz'); function comment_form_add_dz($fields) { $label = __( '地址' ); $value = isset($_POST['dz']) ? $_POST['dz'] : false; $fields['dz'] =<<

HTML; return $fields; } add_filter('comment_form_default_fields','comment_form_add_yb'); function comment_form_add_yb($fields) { $label = __( '邮编' ); $value = isset($_POST['yb']) ? $_POST['yb'] : false; $fields['yb'] =<<

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_action('wp_insert_comment','wp_insert_sfz',10,2); function wp_insert_sfz($comment_ID,$commmentdata) { $sfz = isset($_POST['sfz']) ? $_POST['sfz'] : false; update_comment_meta($comment_ID,'_sfz',$sfz); } add_action('wp_insert_comment','wp_insert_dz',10,2); function wp_insert_dz($comment_ID,$commmentdata) { $dz = isset($_POST['dz']) ? $_POST['dz'] : false; update_comment_meta($comment_ID,'_dz',$dz); } add_action('wp_insert_comment','wp_insert_yb',10,2); function wp_insert_yb($comment_ID,$commmentdata) { $yb = isset($_POST['yb']) ? $_POST['yb'] : false; update_comment_meta($comment_ID,'_yb',$yb); } /** *后台管理页面显示自定义字段 * */ 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; }} add_filter( 'manage_edit-comments_columns', 'my_comments_columns_sfz' ); add_action( 'manage_comments_custom_column', 'output_my_comments_columns_sfz', 10, 2 ); function my_comments_columns_sfz( $columns ){ $columns[ '_sfz' ] = __( '身份证' ); return $columns; } function output_my_comments_columns_sfz( $column_name, $comment_id ){ switch( $column_name ){ case '_sfz'; echo get_comment_meta( $comment_id, '_sfz', true ); break; }} add_filter( 'manage_edit-comments_columns', 'my_comments_columns_dz' ); add_action( 'manage_comments_custom_column', 'output_my_comments_columns_dz', 10, 2 ); function my_comments_columns_dz( $columns ){ $columns[ '_dz' ] = __( '地址' ); return $columns; } function output_my_comments_columns_dz( $column_name, $comment_id ){ switch( $column_name ){ case '_dz'; echo get_comment_meta( $comment_id, '_dz', true ); break; }} add_filter( 'manage_edit-comments_columns', 'my_comments_columns_yb' ); add_action( 'manage_comments_custom_column', 'output_my_comments_columns_yb', 10, 2 ); function my_comments_columns_yb( $columns ){ $columns[ '_yb' ] = __( '邮编' ); return $columns; } function output_my_comments_columns_yb( $column_name, $comment_id ){ switch( $column_name ){ case '_yb'; echo get_comment_meta( $comment_id, '_yb', true ); break; }} /** *删除url评论字段 * */ add_filter('comment_form_default_fields', 'unset_url_field'); function unset_url_field($fields){ if(isset($fields['url'])) unset($fields['url']); return $fields; }

在后台字段中添加了如上的字段。然后就是自定义评论表单的显示。我们编写comment.php文件:



    'ol', 'short_ping' => true, 'avatar_size' => 42, ) ); ?>

'

', 'title_reply_after' => '

', 'title_reply'=>'内容', 'label_submit' => '确认举报' ); comment_form( $args );*/ ?>

上面的comment.php中,我们调用了comment-field.php,我们需要在编写这个文件,来显示页面中的评论表单。

logged in to post a comment.'), get_option('siteurl')."/wp-login.php?redirect_to=".urlencode(get_permalink()));?>




其中包含了验证。然后就能进行正常的显示。

邮件的发送提醒:

由于举报写完,我们需要将所填写的信息通过邮件进行发送到指定的邮箱。我们自己写一个点击确认举报时发送邮件的代码。

首先我们在functions.php中应用我们将要编写的代码youjian.php

FromName = $mail_name ? $mail_name : 'wu'; 
        $phpmailer->Host = $mail_host ? $mail_host : 'smtp.163.com';
        $phpmailer->Port = $mail_port ? $mail_port : '994';
        $phpmailer->Username = $mail_username ? $mail_username : '[email protected]';
        $phpmailer->Password = $mail_passwd ? $mail_passwd : '123456789';
        $phpmailer->From = $mail_username ? $mail_username : '[email protected]';
        $phpmailer->SMTPAuth =  true  ;
        $phpmailer->SMTPSecure = $mail_smtpsecure ? $mail_smtpsecure : 'ssl';
        $phpmailer->IsSMTP();
		
		//测试发送邮件的内容
		$comment = get_comment($comment_id);
		$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
		$spam_confirmed = $comment->comment_approved;
		
		               
        $message = '
		    

Hi ' . trim(get_comment($parent_id)->comment_author) . ':

您有一条留言有了新的回复,摘要信息请见下表。

原文 回复 作者 测试 操作
' . (get_option('comment_author_email')) . ' '. (get_option('tel')) . ' ' . trim($comment->comment_author) . ' ' . $comment_id . ' 查看回复

该邮件由系统自动发出,如果不是您本人操作,请忽略此邮件。

' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '

' . date("Y年m月d日",time()) . '

'; $phpmailer->IsHTML( true ); $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n"; $phpmailer->Body = $message; } } /** * Comments email response system */ add_action('comment_unapproved_to_approved', 'kratos_comment_approved'); function kratos_comment_approved($comment) { if(is_email($comment->comment_author_email)) { $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); $to = trim($comment->comment_author_email); $post_link = get_permalink($comment->comment_post_ID); $subject = '[通知]您的留言已经通过审核'; $message = '

Hi ' . trim($comment->comment_author) . ':

您有一条留言通过了管理员的审核并显示在文章页面,摘要信息请见下表。

文章 内容 操作
《' . get_the_title($comment->comment_post_ID) . '》 '. trim($comment->comment_content) . ' 查看留言

该邮件由系统自动发出,如果不是您本人操作,请忽略此邮件。

' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '

' . date("Y年m月d日",time()) . '

'; $from = "From: \"" . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . "\" <$wp_email>"; $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n"; wp_mail( $to, $subject, $message, $headers ); } } function comment_mail_notify($comment_id) { $comment = get_comment($comment_id); $parent_id = $comment->comment_parent ? $comment->comment_parent : ''; $spam_confirmed = $comment->comment_approved; if (($parent_id != '') && ($spam_confirmed != 'spam')) { $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); $to = trim(get_comment($parent_id)->comment_author_email); $subject = '[通知]您的留言有了新的回复'; $message = '

Hi ' . trim(get_comment($parent_id)->comment_author) . ':

您有一条留言有了新的回复,摘要信息请见下表。

原文 回复 作者 操作
' . trim(get_comment($parent_id)->comment_content) . ' '. trim($comment->comment_content) . ' ' . trim($comment->comment_author) . ' 查看回复

该邮件由系统自动发出,如果不是您本人操作,请忽略此邮件。

' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '

' . date("Y年m月d日",time()) . '

'; $from = "From: \"" . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . "\" <$wp_email>"; $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n"; wp_mail( $to, $subject, $message, $headers ); } } add_action('comment_post', 'comment_mail_notify'); ?>


但是代码在发送邮件的时候出现问题,就是不能够得到前台评论的一些相关信息。mail_smtp( $phpmailer , $comment_id)不能够得到$comment_id的信息,这句是我自己添加的。等明天在找下原因。

还有就是这样不能进行后台订制一些邮箱的信息。明天再一起写一个后台的菜单,来让用户自己填写邮箱的一些信息。


你可能感兴趣的:(wordpress创建自定义后台评论,并且发送邮件)