一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

DEDECMS|帝國CMS|Discuz|PHPCMS|Wordpress|ZBLOG|ECSHOP|蘋果CMS|極致CMS|CMS系統(tǒng)|

服務(wù)器之家 - 建站程序 - Wordpress - wordpress 為主題添加AJAX提交評論功能的php代碼

wordpress 為主題添加AJAX提交評論功能的php代碼

2019-10-07 14:16wordpress教程網(wǎng) Wordpress

前幾天,為了提高評論體驗,為了減輕服務(wù)器負擔(dān),我為主題添加了AJAX提交評論功能,現(xiàn)在分享一下方法。

首先需要在主題的function.php文件里添加一段函數(shù): 

復(fù)制代碼

代碼如下:


< ?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; 
// Check DB 
if(!$wpdb->dbh) { 
echo('Our database has issues. Try again later.'); 
die(); 

nocache_headers(); 
$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']); 
// If the user is logged in 
$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); 
if ( current_user_can('unfiltered_html') ) { 
if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) { 
kses_remove_filters(); // start with a clean slate 
kses_init_filters(); // set up the filters 


} else { 
if ( get_option('comment_registration') ) 
fail('Sorry, you must be logged in to post a comment.'); 

$comment_type = ''; 
if ( get_option('require_name_email') && !$user->ID ) { 
if ( 6> strlen($comment_author_email) || '' == $comment_author ) 
fail('Sorry: please fill the required fields (name, email).'); 
elseif ( !is_email($comment_author_email)) 
fail('Sorry: please enter a valid email address.'); 

if ( '' == $comment_content ) 
fail('Sorry: please type a comment.'); 
// Simple duplicate check 
$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' "; 
if ( $comment_author_email ) $dupe .= "OR comment_author_email = '$comment_author_email' "; 
$dupe .= ") AND comment_content = '$comment_content' LIMIT 1"; 
if ( $wpdb->get_var($dupe) ) { 
fail('Duplicate comment detected; it looks as though you\'ve already said that!'); 

$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID'); 
if( !$user->ID ){ 
$result_set = $wpdb->get_results("SELECT display_name, user_email FROM $wpdb->users WHERE display_name = '" . $comment_author . "' OR user_email = '" . $comment_author_email . "'"); 
if ($result_set) { 
if ($result_set[0]->display_name == $comment_author){ 
fail( __('Error: you are not allowed to use the nickname that you entered.if you are the administrator you hava to login to comment.','philna2') ); 
} else { 
fail( __('Error: you are not allowed to use the email that you entered.if you are the administrator you hava to login to comment.','philna2') ); 



$comment_id = wp_new_comment( $commentdata ); 
$comment = get_comment($comment_id); 
if( !$user->ID ){ 
setcookie('comment_author_' . COOKIEHASH, $comment->comment_author, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); 
setcookie('comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); 
setcookie('comment_author_url_' . COOKIEHASH, clean_url($comment->comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); 

@header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset')); 
?> 
//這里需要粘貼你的評論框架代碼,不過相關(guān)的調(diào)用代碼有所變化: 
//評論ID:$comment->comment_ID 
//評論者名字:$comment->comment_author 
//判斷評論者是否填寫了網(wǎng)站地址:$comment->get_comment_author_url 
//評論者URL:$comment->comment_author_url 
//評論時間:mysql2date(__('F jS, Y'),$comment->comment_date) 
//評論者e-mail:$comment->comment_author_email 
//評論內(nèi)容$comment->comment_content 
< ?php 
die(); 


add_action('init', 'ajax_comment'); 
//添加AJAX評論鉤子 
?> 


如果處理評論內(nèi)容里的換行符的話,$comment->comment_content需做以下處理: 

復(fù)制代碼

代碼如下:


< ?php 
$aj_order = array("\r\n", "\n", "\r"); 
$aj_comment_content = str_replace($aj_order,'
',$comment->comment_content); 
echo $aj_comment_content; 
?> 


用jQuery寫提交評論時的腳本,這也是關(guān)鍵的部分: 

復(fù)制代碼

代碼如下:


jQuery(document).ready(function() { 
if (jQuery('#commentform').length) { 
jQuery('#commentform').submit(function(){ //ID為 commentform 的表單提交時發(fā)生的函數(shù),也就是整個留言輸入框 form 的ID。 
var ajaxCommentsURL = window.location.href; 
jQuery.ajax({ 
url: ajaxCommentsURL, 
data: jQuery('#commentform').serialize()+'&action=ajax_comment', 
type: 'POST', 
beforeSend: function() { 
jQuery('#commenterror').hide(); 
var submit='<div id="commentload" style="display: none;background: url("img/spinner.gif") no-repeat scroll 0 50%;margin: 0 auto;">Submitting Comment...</div>'; //創(chuàng)建名為 submit 的字符串,稍后插入,這里的樣式大家自己根據(jù)需要定義,那個背景圖片自己去下哈。 
var error='<div id="commenterror" style="display: none;margin: 0 auto;">Posted comment fail.</div>'; //創(chuàng)建名為 error 的字符串 
jQuery('#comments').after(submit); // 在ID為 comments 的元素后插入剛定義的 submit 
jQuery('#comments').after(error); // 同樣插入剛定義的 error 
jQuery('#commentload').slideDown(); // 讓submit 向下滑出 
}, 
error: function(request) { //發(fā)生錯誤時 
jQuery('#commentload').hide(); //隱藏 submit 
jQuery('#commenterror').show("slow").html(request.responseText); //顯示 error 
}, 
success: function(data) { 
jQuery('textarea').each(function(){ 
this.value=''; 
}); 
jQuery('#commenterror').hide().html(); 
if (!jQuery('#thecomments').length) { 
jQuery('#pinglist').before('<ol id="thecomments"></ol>');} 
jQuery('#thecomments').append(data); //向ID為 thecomments 的元素添加數(shù)據(jù),也就是整個 ol 或 ul 
var new_comment = jQuery('#thecomments li:last').hide(); //讓最新添加的數(shù)據(jù)隱藏 
new_comment.slideDown(1000); //再顯示,這里是為了實現(xiàn)滑出的效果,不想要也可以直接顯示 
jQuery('#commentform:input').attr('disabled', true); 
jQuery('#commentload').slideUp("slow"); 
jQuery('#messagebox').slideUp("slow"); //這是針對我模版而加的,因為我模版在沒有留言時會有個 nocomment 的元素,我要讓添加一條留言后他自動隱藏,要不然會矛盾,呵呵,這個可以自行選擇要或不要 
setTimeout(function() { 
jQuery('#commentform:input').removeAttr('disabled'); 
}, 10000); //這里是設(shè)置10秒之后才可以再次留言,自行設(shè)置,單位毫秒。 

}); 
return false; 
} ); 
}}) 


注意: 
腳本里的對象ID要對應(yīng)主題里的評論相關(guān)框架的ID。 
后續(xù)步驟: 
為commentload,commenterror寫CSS。 
有待完善的地方: 
AJAX提交后的評論序號顯示為1。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产乱子伦真实china | 久久国产精品免费网站 | 欧美成人禁片在线观看俄罗斯 | 亚洲ⅴa偷拍在线影院 | 精品亚洲综合在线第一区 | 四虎影院新网址 | 男人天堂色 | 亚洲精品国产精麻豆久久99 | 精品视频在线播放 | 女王厕便器vk | 免费aⅴ片| 69pao强力打造免费高速 | 4tube欧美高清 | 欧美国产日本高清不卡 | 97视频免费人人观看人人 | 2048论坛永久入口 原创合集 | 五月香婷婷 | 五月丁开婷婷 | 按摩院已婚妇女中文字幕 | 国产一卡二卡3卡4卡四卡在线 | 亚洲AV无码A片在线观看蜜桃 | 日本高清免费不卡在线播放 | 欧美xxxxx69 | 亚洲视频中文 | 高清国产在线观看 | 白丝爆动漫羞羞动漫软件 | 99精彩视频 | 共妻高h| 色猪视频| 秋霞理论最新三级理论最 | 欧美视频精品一区二区三区 | 毛片免费观看 | 操大肥b | kk4444在线影视播放 | 亚洲haose在线观看 | cos美女被黄网站 | 侵犯小男生免费视频网站 | 亚洲视频一区网站 | 四虎网址在线 | 楚乔传第二部免费播放电视连续剧 | 公妇乱淫在线播放免费观看 |