日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

wordpressPHP实现ajax评论,AJAX_wordpress 为主题添加AJAX提交评论功能的php代码,首先需要在主题的function.php文 - phpStudy...

發布時間:2025/3/21 php 63 豆豆
生活随笔 收集整理的這篇文章主要介紹了 wordpressPHP实现ajax评论,AJAX_wordpress 为主题添加AJAX提交评论功能的php代码,首先需要在主题的function.php文 - phpStudy... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

復制代碼代碼如下:

< ?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'));

?>

//這里需要粘貼你的評論框架代碼,不過相關的調用代碼有所變化:

//評論ID:$comment->comment_ID

//評論者名字:$comment->comment_author

//判斷評論者是否填寫了網站地址:$comment->get_comment_author_url

//評論者URL:$comment->comment_author_url

//評論時間:mysql2date(__('F jS, Y'),$comment->comment_date)

//評論者e-mail:$comment->comment_author_email

//評論內容$comment->comment_content

< ?php

die();

}

}

add_action('init', 'ajax_comment');

//添加AJAX評論鉤子

?>

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

復制代碼代碼如下:

< ?php

$aj_order = array("\r\n", "\n", "\r");

$aj_comment_content = str_replace($aj_order,'

',$comment->comment_content);

echo $aj_comment_content;

?>

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

復制代碼代碼如下:

jQuery(document).ready(function() {

if (jQuery('#commentform').length) {

jQuery('#commentform').submit(function(){ //ID為 commentform 的表單提交時發生的函數,也就是整個留言輸入框 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='

Submitting Comment...'; //創建名為 submit 的字符串,稍后插入,這里的樣式大家自己根據需要定義,那個背景圖片自己去下哈。

var error='

Posted comment fail.'; //創建名為 error 的字符串

jQuery('#comments').after(submit); // 在ID為 comments 的元素后插入剛定義的 submit

jQuery('#comments').after(error); // 同樣插入剛定義的 error

jQuery('#commentload').slideDown(); // 讓submit 向下滑出

},

error: function(request) { //發生錯誤時

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('

');}

jQuery('#thecomments').append(data); //向ID為 thecomments 的元素添加數據,也就是整個 ol 或 ul

var new_comment = jQuery('#thecomments li:last').hide(); //讓最新添加的數據隱藏

new_comment.slideDown(1000); //再顯示,這里是為了實現滑出的效果,不想要也可以直接顯示

jQuery('#commentform:input').attr('disabled', true);

jQuery('#commentload').slideUp("slow");

jQuery('#messagebox').slideUp("slow"); //這是針對我模版而加的,因為我模版在沒有留言時會有個 nocomment 的元素,我要讓添加一條留言后他自動隱藏,要不然會矛盾,呵呵,這個可以自行選擇要或不要

setTimeout(function() {

jQuery('#commentform:input').removeAttr('disabled');

}, 10000); //這里是設置10秒之后才可以再次留言,自行設置,單位毫秒。

}

});

return false;

} );

}})

注意:

腳本里的對象ID要對應主題里的評論相關框架的ID。

后續步驟:

為commentload,commenterror寫CSS。

有待完善的地方:

AJAX提交后的評論序號顯示為1。

總結

以上是生活随笔為你收集整理的wordpressPHP实现ajax评论,AJAX_wordpress 为主题添加AJAX提交评论功能的php代码,首先需要在主题的function.php文 - phpStudy...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 青草久久久 | 一区二区三区四区免费 | 大胸美女无遮挡 | 狠狠五月 | 日韩欧美专区 | 在线观看视频一区二区三区 | 蜜桃久久久aaaa成人网一区 | 精品视频一区二区三区四区五区 | 三年中文在线观看免费观看 | 国产三级做爰高清在线 | aav在线| 久久久久久麻豆 | 依人在线视频 | 久久永久视频 | 拍真实国产伦偷精品 | 哈利波特3在线观看免费版英文版 | 日韩毛片在线观看 | 欧美精品乱码久久久久久 | 午夜免费视频 | 午夜影院污 | 国产精品入口麻豆九色 | 欧美 日韩 中文字幕 | 老司机av导航 | 性欧美videos另类hd | 国产不卡在线视频 | 男男肉耽高h彩漫 | 国产高清精品软件丝瓜软件 | 天天插天天狠 | 欧美黄片一区 | 国产一区二区三区免费观看 | 538国产精品一区二区 | 亚洲女人被黑人巨大进入 | 欧美在线一区视频 | 久久精品国产亚洲av麻豆色欲 | 国产 日韩 欧美 精品 | 精品久久人妻av中文字幕 | 精品无码国产污污污在线观看 | 综合久色| 成人亚洲网| 看一级片 | 国产喷白浆一区二区三区 | 国产九色91回来了 | 99精品欧美一区二区三区综合在线 | 久久久久久久久精 | 成人午夜av在线 | 欧美裸体xxxx | 免费一级片网站 | 欧美精品99久久 | av黄色网| 999国产| 激情视频久久 | 噼里啪啦动漫高清在线观看 | 美国伊人网 | 日日碰狠狠添天天爽无码av | 99免费视频| 男女午夜视频在线观看 | 天海翼一二三区 | 国产毛片在线看 | 久久亚 | 女同调教视频 | 少妇精品无码一区二区免费视频 | 91日批| 视频国产精品 | 三级视频在线看 | 五月涩 | 午夜精品视频在线观看 | 中文字幕婷婷 | 精品欧美视频 | sesese99| 青青伊人av | 日韩毛片在线免费观看 | 色窝窝综合色窝窝久久 | 天堂在线中文资源 | 久久久久亚洲色欲AV无码网站 | 亚洲一区二区三区久久久成人动漫 | 在线观看亚洲精品视频 | 天天影视插插插 | 久久噜噜色综合一区二区 | 国产成人综合在线 | 尤物网站在线播放 | av看片网站 | 天堂视频在线免费观看 | 国产一区视频观看 | 日韩专区第一页 | av一区不卡| 日韩黄色视屏 | 国产精品拍拍 | 欧美日韩成人在线视频 | 99久久亚洲精品日本无码 | 红桃成人网 | 一级特黄aa大片欧美 | eeuss鲁片一区二区三区在线观看 | 毛片a级片 | 永久免费看片在线观看 | 污动漫网站 | 香蕉视频在线免费看 | 久久久久亚洲日日精品 | 久久久国产网站 | 国产一区二区三区自拍 |