wordpress网站Ajax留言评论+自定义评论字段
前端代码,下面的电话,公司,为自定义字段。
<form method="post" id="commentform" class="comment-form shansubmit" ><lable>用户</lable><input id="author" type="text" name="comment_author" value=""/> <br><lable>邮箱</lable><input id="email" type="text" name="comment_author_email" value=""/> <br><lable>电话</lable><input id="phone" type="text" name="comment_author_phone" value=""/> <br><lable>公司</lable><input id="company" type="text" name="comment_author_company" value=""/> <br><lable>内容</lable><textarea id="comment" name="comment" required="required"><?echo $message;?></textarea> <br><lable>url </lable><input id="url" name="comment_author_url" value="<? echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] ?>"><br><lable>ID</lable><input name="comment_post_ID" value="<? echo get_the_ID() ?>" id="comment_post_ID"> <br><lable>IP</lable><input name="comment_author_IP" value="<? echo $_SERVER['REMOTE_ADDR']; ?>" id="comment_author_IP"> <br><input name="submit" type="submit" id="submit" class="shansubmit" value="发表评论">
</form>
js代码
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/style/js/jquery-3.4.1.min.js"></script>
<script> var ajax_url = '<?php echo admin_url('admin-ajax.php'); ?>'; </script>
<script type="text/javascript">
jQuery(document).ready(function($) {$('#commentform').on('submit', function(e) {e.preventDefault();var commentData = $(this).serialize(); // 获取评论表单数据// 发送Ajax请求$.ajax({type: 'POST',url: ajax_url, // 后端处理Ajax的URL,需要在wp_localize_script中定义data: {action: 'submit_custom_comment',comment_data: commentData,},success: function(response) {// 处理成功响应console.log(response);}});});
});
</script>
function.php代码
添加Ajax处理函数
// 添加Ajax处理函数
function submit_custom_comment() {if (isset($_POST['comment_data'])) {parse_str($_POST['comment_data'], $commentData); // 解析评论数据// 构建评论数据数组$comment_args = array('comment_post_ID' => $commentData['comment_post_ID'],'comment_author_url' => $commentData['comment_author_url'],'comment_author' => $commentData['comment_author'],'comment_author_email' => $commentData['comment_author_email'],'comment_content' => $commentData['comment'],'comment_type' => 'comment','comment_meta' => array('phone' => $commentData['comment_author_phone'], // 将自定义字段添加到评论元数据中'company' => $commentData['comment_author_company'], // 将自定义字段添加到评论元数据中));// 插入评论$comment_id = wp_insert_comment($comment_args);if ($comment_id) {echo '提交成功';} else {echo '提交失败';}}die(); // 必须终止脚本
}
add_action('wp_ajax_submit_custom_comment', 'submit_custom_comment'); // 钩子 - 处理登录用户的请求
add_action('wp_ajax_nopriv_submit_custom_comment', 'submit_custom_comment'); // 钩子 - 处理非登录用户的请求
添加后台评论处显示自定义字段代码
add_filter( 'manage_edit-comments_columns', 'my_comments_columns' );
add_action( 'manage_comments_custom_column', 'output_my_comments_columns', 10, 3 );function my_comments_columns( $columns ){$columns[ 'phone' ] = __( '电话' ); $columns[ 'company' ] = __( '公司' ); return $columns;
}function output_my_comments_columns( $column_name, $comment_id ){switch( $column_name ) {case "phone" :echo get_comment_meta( $comment_id, 'phone', true );break;case "company" :echo get_comment_meta( $comment_id, 'company', true );break;}
}