最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WordPress中利用AJAX技術(shù)進行評論提交的實現(xiàn)示例

 更新時間:2016年01月12日 14:47:46   作者:稍息少年  
這篇文章主要介紹了WordPress中利用AJAX技術(shù)進行評論提交的實現(xiàn)示例,整個處理的關(guān)鍵點在于文中的ajax_comment函數(shù),需要的朋友可以參考下

一直對 WordPress 的 Ajax 交互研究感興趣,也一直很關(guān)注于這方面的技術(shù),談到 WordPress Ajax 就不得不談到評論 Ajax提交,作為一個博客、論壇評論的 Ajax 提交不僅可以改善用戶體驗,還可以大幅縮減服務(wù)器開支,畢竟輸出單條評論內(nèi)容比重新組織輸出一個頁面要簡單的多。 雖說現(xiàn)在訪問量一直比較低,不存在服務(wù)器壓力的問題,但一向注重用戶體驗的我,當然不能放棄這么一個提升用戶體驗的機會。今天抽了一下午的空,把這個主題的 Ajax 評論提交初步完成了。

直接開門見山,直接上代碼:(原理及思路在最后)
根據(jù)自己主題不同結(jié)構(gòu),以下代碼請自行調(diào)整。

WordPress Ajax 提交評論 PHP 代碼
在主題 function.php 文件中加入如下部分。

//以下大部分代碼出自 yinheli 經(jīng)由該部分代碼,排除部分錯誤、優(yōu)化精簡得出以下代碼。
//yinheli博客不做了,所以這里就不給鏈接了。
//Edited by XiangZi DEC.17TH 2011
function fail($s) {//虛擬錯誤頭部分
  header('HTTP/1.0 500 Internal Server Error');
  echo $s;
  exit;
}
function ajax_post_comment_slow (){
 fail('用不用說這么快?想好了再說!');
}
//評論太快輸出代碼。
add_filter('comment_flood_trigger','ajax_post_comment_slow', 0);
//掛一個評論太快,返回內(nèi)容的鉤子
function ajax_comment(){
// Ajax php 響應(yīng)部分代碼
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) ) {
//這一套判斷貌似抄的 wp 源代碼 。詳見:include/comment.php
  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('火星人?注冊個?');
}
$comment_type = '';
if ( get_option('require_name_email') && !$user->ID ) {
  if ( 6> strlen($comment_author_email) || '' == $comment_author )
    fail('Oopps,名字[Name]或郵箱[email]不對。');
  elseif ( !is_email($comment_author_email))
    fail('Oopps,郵箱地址[Email]不對。');
}
if ( '' == $comment_content )
  fail('是不是應(yīng)該寫點什么再提交?');
// 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('評論重復了!有木有!');
}
$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('博主你也敢冒充?');
 } else {
 fail('博主你也敢冒充?');
 }
 }
}
$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'));
 xz_comment($comment, null);//這是我的調(diào)用評論函數(shù),換成你的函數(shù)名。
 die();
}
}
add_action('init', 'ajax_comment');

Javascript 中代碼
注意:以下代碼需要 Jquery 框架支援。
javascript onload 代碼中加入以下部分。

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();
        jQuery('#commentload').fadeIn();
      },
      error: function(request) {  //發(fā)生錯誤時
        jQuery('#commenterror').html(request.responseText);
        jQuery('#commentload').hide();  //隱藏 submit
        jQuery('#commenterror').fadeIn(); //顯示 error 
      },
      success: function(data) {
        jQuery('textarea').each(function(){
          this.value='';
        });
        jQuery('#commenterror').fadeOut();
        if(jQuery(".commentlist li.comment").first().length != 0){jQuery(".commentlist li.comment").first().before(data)}  
        else {jQuery("ol.commentlist").append(data)}
        jQuery(".commentlist li.comment").first().hide(0,function(){$(this).slideDown(1000)});
        jQuery('#cmt-submit').attr('disabled', true).css({"background-color":"#6C6C6C","color":"#E0E0E0"});
        jQuery('#commentload').fadeOut(1600);
 setTimeout(function() {
        jQuery('#cmt-submit').removeAttr('disabled').css({"background-color":"#0086C5","color":"#FFFFFF"});
        },3000); 
      }
    });
    return false;
  } );
}

注:代碼仍有改進需求,因為沒有時間,所以就沒有再進化。

CSS 代碼
css 隨意部分添加。

#commentload,#commenterror{
 display: none;
 margin: 5px 0 0 0;
 color:#D29A04;
 float: left;
 font-size:16px;
 padding:0 0 0 20px;
}
#commentload{
 background: url("img/loading.gif") no-repeat bottom left ;
}
#commenterror{
 background: url("img/error.png") no-repeat bottom left ;
}

原理、思路
原理:
Javascript 提交數(shù)據(jù)
php響應(yīng)并輸出結(jié)果
Javascript 得到結(jié)果并顯示
思路:
點擊提交按鈕后,Javascript 截獲提交動作
截獲提交的各項數(shù)據(jù)(Name、Email、Web、Comment-text)
利用 Javascript Jquery 模擬瀏覽器提交POST(Name、Email、Web、Comment-text)請求之WordPress
Function.php 文件中構(gòu)造一個接受請求的函數(shù),即本列中ajax_comment函數(shù)
如果請求無錯誤,輸出正確結(jié)果
如果請求有錯誤,輸出錯誤結(jié)果
Javascript 獲得正確結(jié)果,動態(tài)添加到評論列表中
Javascript 獲得錯誤結(jié)果,動態(tài)添加到提交提示欄
改進
樣式方面,我確實沒什么美感,所以正在學習中。
提交按鈕在點擊至獲得返回結(jié)果后3秒的時間里應(yīng)該都是變灰失效狀態(tài),這一點之前因為在本機測試,提交瞬間完成沒有注意到,遠程測試的時候發(fā)現(xiàn)了,但要改的話還要進行測試,時間太緊就不改了,有機會再改進一下。

總結(jié)
因為 WordPress 主題中評論樣式的自由性、多樣性,所以貌似至今一直沒有一款通用性的AJAX 評論插件,
一些高手也只能在優(yōu)化自己博客之余,把思路和部分通用核心代碼做一下公布,
所以想要實現(xiàn)一些炫酷的功能要不有高人幫你,
要不你就只能好好學代碼,期待有一日能夠厚積薄發(fā)了。
效果請自行提交評論驗證。

相關(guān)文章

  • 微信小程序?qū)崿F(xiàn)商品分類頁過程結(jié)束

    微信小程序?qū)崿F(xiàn)商品分類頁過程結(jié)束

    這篇文章主要為大家詳細介紹了微信小程序?qū)崿F(xiàn)商品分類頁列表方法,商品分類頁主要是需要實現(xiàn)商品類目和對應(yīng)商品標題的聯(lián)動跳轉(zhuǎn),文中過程詳細,感興趣的小伙伴們可以參考一下
    2023-05-05
  • 使用TypeScript和裝飾器實現(xiàn)前端數(shù)據(jù)脫敏

    使用TypeScript和裝飾器實現(xiàn)前端數(shù)據(jù)脫敏

    這篇文章主要為大家詳細介紹了如何使用TypeScript和裝飾器實現(xiàn)前端數(shù)據(jù)脫敏功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2024-11-11
  • innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等區(qū)別

    innertext , insertadjacentelement , insertadjacenthtml , ins

    innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等區(qū)別...
    2007-06-06
  • ES6新特性五:Set與Map的數(shù)據(jù)結(jié)構(gòu)實例分析

    ES6新特性五:Set與Map的數(shù)據(jù)結(jié)構(gòu)實例分析

    這篇文章主要介紹了ES6新特性五之Set與Map的數(shù)據(jù)結(jié)構(gòu),結(jié)合實例形式分析了ES6中Set與Map的功能、定義、屬性、結(jié)構(gòu)與相關(guān)使用技巧,需要的朋友可以參考下
    2017-04-04
  • JavaScript自定義Promise實現(xiàn)流程

    JavaScript自定義Promise實現(xiàn)流程

    現(xiàn)在網(wǎng)上有非常多的Promise文章,但都是給你一堆代碼,或者某些核心代碼,讓你看完之后感覺,嗯,很厲害,但還是不知所云,不知其所以然。那么本文真正從一個小白開始帶你深入淺出,一步一步實現(xiàn)自己的?Promise,這種自己造輪子的過程一定是進步最快的過程,快上車開始吧
    2022-10-10
  • WordPress 單頁面上一頁下一頁的實現(xiàn)方法【附代碼】

    WordPress 單頁面上一頁下一頁的實現(xiàn)方法【附代碼】

    下面小編就為大家?guī)硪黄猈ordPress 單頁面上一頁下一頁的實現(xiàn)方法【附代碼】。小編覺得非常不錯。給大家分享一下。希望能給大家一個參考。
    2016-03-03
  • JavaScript Map實現(xiàn)原理與底層結(jié)構(gòu)詳解

    JavaScript Map實現(xiàn)原理與底層結(jié)構(gòu)詳解

    哈希表(也稱為哈希表)是一種基于鍵直接訪問內(nèi)存存儲位置的數(shù)據(jù)結(jié)構(gòu)。也就是說,它通過計算一個鍵值函數(shù)來加速查找,該函數(shù)將要查詢的數(shù)據(jù)映射到表中的某個位置。該映射函數(shù)稱為散列函數(shù),記錄數(shù)組稱為散列表
    2022-09-09
  • javascript日期對象格式化為字符串的實現(xiàn)方法

    javascript日期對象格式化為字符串的實現(xiàn)方法

    本篇文章主要是對javascript日期對象格式化為字符串的實現(xiàn)方法進行了詳細的介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • JS+html5 canvas實現(xiàn)的簡單繪制折線圖效果示例

    JS+html5 canvas實現(xiàn)的簡單繪制折線圖效果示例

    這篇文章主要介紹了JS+html5 canvas實現(xiàn)的簡單繪制折線圖效果,結(jié)合實例形式分析了js結(jié)合HTML5 canvas技術(shù)實現(xiàn)圖形繪制的數(shù)值運算與數(shù)組遍歷等操作技巧,需要的朋友可以參考下
    2017-03-03
  • Electron實現(xiàn)文件復制到剪切板的方案

    Electron實現(xiàn)文件復制到剪切板的方案

    這篇文章主要介紹了Electron實現(xiàn)文件復制到剪切板的解決方案,文中通過代碼示例講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-11-11

最新評論

三穗县| 陈巴尔虎旗| 巴东县| 高陵县| 饶阳县| 漳州市| 左贡县| 饶平县| 萨嘎县| 保德县| 西青区| 夏邑县| 临沂市| 龙山县| 乌苏市| 洪江市| 名山县| 马公市| 昌乐县| 闽侯县| 紫阳县| 饶阳县| 文成县| 河曲县| 怀安县| 大连市| 福海县| 东乡族自治县| 青神县| 和田县| 九江县| 潮州市| 遵义县| 江陵县| 乐清市| 鲜城| 施甸县| 晋州市| 萨嘎县| 庆元县| 遵化市|