在WordPress中,为主题添加AJAX提交评论功能可以极大地提升用户体验,因为它允许用户在不刷新整个页面的情况下发送评论,从而减少了服务器的负载。下面将详细解释如何实现这一功能。 你需要在你的主题的`functions.php`文件中添加一个PHP函数来处理AJAX请求。这个函数通常会检查提交的数据,并在必要时对评论进行验证和处理。以下是一个基本的示例: ```php function fail($s) { header('HTTP/1.0 500 Internal Server Error'); echo $s; exit; } function ajax_comment() { if ($_POST['action'] == 'ajax_comment') { global $wpdb, $db_check; // 检查数据库连接 if (!$wpdb->dbh) { echo('Our database has issues. Try again later.'); die(); } nocache_headers(); // 获取评论的帖子ID $comment_post_ID = (int) $_POST['comment_post_ID']; // 获取帖子和评论状态 $status = $wpdb->get_row("SELECT post_status, comment_status FROM $wpdb->posts WHERE ID ='$comment_post_ID'"); // 验证评论状态 if (empty($status->comment_status)) { do_action('comment_id_not_found', $comment_post_ID); fail('The post you are trying to comment on does not currently exist in the database.'); } elseif ('closed' == $status->comment_status) { do_action('comment_closed', $comment_post_ID); fail('Sorry, comments are closed for this item.'); } elseif (in_array($status->post_status, array('draft', 'pending'))) { do_action('comment_on_draft', $comment_post_ID); fail('The post you are trying to comment on has not been published.'); } // 获取评论作者信息 $comment_author = trim(strip_tags($_POST['author'])); $comment_author_email = trim($_POST['email']); $comment_author_url = trim($_POST['url']); $comment_content = trim($_POST['comment']); // 如果用户已登录 $user = wp_get_current_user(); if ($user->ID) { $comment_author = $wpdb->escape($user->display_name); $comment_author_email = $wpdb->escape($user->user_email); $comment_author_url = $wpdb->escape($user->user_url); // 检查无过滤HTML权限 if (current_user_can('unfiltered_html')) { if (wp_create_nonce('unfiltered_html_comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment']) { kses_remove_filters(); // 移除所有kses过滤器 kses_init_filters(); // 重新初始化kses过滤器 } } } else { // 如果需要注册才能评论 if (get_option('comment_registration')) { fail('Sorry, you must be logged in to post a comment.'); } } $comment_type = ''; // 更多的评论验证和处理逻辑可以在这里添加 // ... // 如果所有验证都通过,可以调用WordPress的内置函数创建评论 $new_comment_id = wp_new_comment(array( 'comment_post_ID' => $comment_post_ID, 'comment_author' => $comment_author, 'comment_author_email' => $comment_author_email, 'comment_author_url' => $comment_author_url, 'comment_content' => $comment_content, 'comment_type' => $comment_type, )); // 返回新评论的ID echo $new_comment_id; exit; } } ``` 在添加上述函数后,你需要在JavaScript中设置AJAX请求来调用这个PHP函数。你可以创建一个`comment-form.js`文件,并将其包含在你的主题中,例如在`footer.php`文件中。以下是一个简单的JavaScript示例: ```javascript jQuery(document).ready(function($) { $('form#commentform').submit(function(event) { event.preventDefault(); // 阻止表单默认提交 var formData = $(this).serialize(); // 获取表单数据 $.ajax({ type: 'POST', url: '/wp-admin/admin-ajax.php', // WordPress AJAX处理器 data: formData + '&action=ajax_comment', // 添加行动标识 success: function(response) { // 在这里处理成功响应,例如更新评论区域 console.log('Comment submitted successfully! ID:', response); }, error: function(jqXHR, textStatus, errorThrown) { // 处理错误 console.error('Error submitting comment:', textStatus, ', ', errorThrown); }, }); }); }); ``` 确保在主题的`functions.php`文件中注册并激活这个JS文件,可以使用`wp_enqueue_script`函数: ```php function my_theme_scripts() { wp_enqueue_script('comment-form-js', get_template_directory_uri() . '/js/comment-form.js', array('jquery'), '1.0', true); } add_action('wp_enqueue_scripts', 'my_theme_scripts'); ``` 完成这些步骤后,你应该已经成功地为你的WordPress主题添加了AJAX评论提交功能。用户现在可以在不刷新页面的情况下提交评论,同时服务器的负载也得到了减轻。当然,这只是一个基础实现,你可能还需要根据自己的需求进行更多的定制,例如添加错误处理、评论审核机制、防止垃圾评论等。





























- 粉丝: 8
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 如何在EXCEL中怎么输入各种字符.doc
- 5报文摘要算法的研究与实现-信息加密.docx
- 宁乐购购物网站实施方案书方案设计书2.doc
- 简述网络信息安全防护体系——朱节中.docx
- PLC无塔供水大学本科方案设计书2.doc
- 王雪斌-基于PLC的水暖锅炉控制系统改造设计.doc
- 计算机网络专业实习报告.docx
- 区块链技术将带来全方位变革.docx
- 基于PLC三层电梯控制系统的方案设计书.doc
- 交互设计的理论与实践精髓
- 2010年1月自考Java语言程序设计(一)试题.doc
- CADCAM综合训练子项目任务书.doc
- 国有林场计算机信息化建设及管理探析.docx
- 会计人员应对人工智能冲击的对策探索.docx
- Socket网络聊天系统开发与设计方案.doc
- 市政工程项目管理施工中进度控制要点剖析.docx


