ppsurcao 2019-12-15
ä¸ç´å¯¹ WordPress ç Ajax 交äºç 究æå´è¶£ï¼ä¹ä¸ç´å¾å³æ³¨äºè¿æ¹é¢çææ¯ï¼è°å° WordPress Ajax å°±ä¸å¾ä¸è°å°è¯è®º Ajaxæ交ï¼ä½ä¸ºä¸ä¸ªå客ã论åè¯è®ºç Ajax æ交ä¸ä»å¯ä»¥æ¹åç¨æ·ä½éªï¼è¿å¯ä»¥å¤§å¹ç¼©åæå¡å¨å¼æ¯ï¼æ¯ç«è¾åºåæ¡è¯è®ºå容æ¯éæ°ç»ç»è¾åºä¸ä¸ªé¡µé¢è¦ç®åçå¤ã è½è¯´ç°å¨è®¿é®éä¸ç´æ¯è¾ä½ï¼ä¸åå¨æå¡å¨ååçé®é¢ï¼ä½ä¸å注éç¨æ·ä½éªçæï¼å½ç¶ä¸è½æ¾å¼è¿ä¹ä¸ä¸ªæåç¨æ·ä½éªçæºä¼ãä»å¤©æ½äºä¸ä¸åç空ï¼æè¿ä¸ªä¸»é¢ç Ajax è¯è®ºæ交åæ¥å®æäºã
ç´æ¥å¼é¨è§å±±ï¼ç´æ¥ä¸ä»£ç ï¼ï¼åçåæè·¯å¨æåï¼
æ ¹æ®èªå·±ä¸»é¢ä¸åç»æï¼ä»¥ä¸ä»£ç 请èªè¡è°æ´ã
WordPress Ajax æ交è¯è®º PHP 代ç
å¨ä¸»é¢ function.php æ件ä¸å å¥å¦ä¸é¨åã
//以ä¸å¤§é¨å代ç åºèª yinheli ç»ç±è¯¥é¨å代ç ï¼æé¤é¨åé误ãä¼åç²¾ç®å¾åºä»¥ä¸ä»£ç ã //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); //æä¸ä¸ªè¯è®ºå¤ªå¿«ï¼è¿åå容çé©å function ajax_comment(){ // Ajax php ååºé¨å代ç 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('æ¯ä¸æ¯åºè¯¥åç¹ä»ä¹åæ交ï¼'); // 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);//è¿æ¯æçè°ç¨è¯è®ºå½æ°ï¼æ¢æä½ çå½æ°åã die(); } } add_action('init', 'ajax_comment');
Javascript ä¸ä»£ç
注æï¼ä»¥ä¸ä»£ç éè¦ Jquery æ¡æ¶æ¯æ´ã
javascript onload 代ç ä¸å å¥ä»¥ä¸é¨åã
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(); jQuery('#commentload').fadeIn(); }, error: function(request) { //åçéè¯¯æ¶ 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 æ交æ°æ®
phpååºå¹¶è¾åºç»æ
Javascript å¾å°ç»æ并æ¾ç¤º
æè·¯ï¼
ç¹å»æ交æé®åï¼Javascript æªè·æ交å¨ä½
æªè·æ交çå项æ°æ®ï¼NameãEmailãWebãComment-textï¼
å©ç¨ Javascript Jquery 模ææµè§å¨æ交POSTï¼NameãEmailãWebãComment-textï¼è¯·æ±ä¹WordPress
Function.php æ件ä¸æé ä¸ä¸ªæ¥å请æ±çå½æ°ï¼å³æ¬åä¸ajax_commentå½æ°
å¦æ请æ±æ é误ï¼è¾åºæ£ç¡®ç»æ
å¦æ请æ±æé误ï¼è¾åºé误ç»æ
Javascript è·å¾æ£ç¡®ç»æï¼å¨ææ·»å å°è¯è®ºå表ä¸
Javascript è·å¾é误ç»æï¼å¨ææ·»å å°æ交æ示æ
æ¹è¿
æ ·å¼æ¹é¢ï¼æç¡®å®æ²¡ä»ä¹ç¾æï¼æ以æ£å¨å¦ä¹ ä¸ã
æ交æé®å¨ç¹å»è³è·å¾è¿åç»æå3ç§çæ¶é´éåºè¯¥é½æ¯åç°å¤±æç¶æï¼è¿ä¸ç¹ä¹åå 为å¨æ¬æºæµè¯ï¼æ交ç¬é´å®æ没æ注æå°ï¼è¿ç¨æµè¯çæ¶ååç°äºï¼ä½è¦æ¹çè¯è¿è¦è¿è¡æµè¯ï¼æ¶é´å¤ªç´§å°±ä¸æ¹äºï¼ææºä¼åæ¹è¿ä¸ä¸ã
æ»ç»
å 为 WordPress 主é¢ä¸è¯è®ºæ ·å¼çèªç±æ§ãå¤æ ·æ§ï¼æ以è²ä¼¼è³ä»ä¸ç´æ²¡æä¸æ¬¾éç¨æ§çAJAX è¯è®ºæ件ï¼
ä¸äºé«æä¹åªè½å¨ä¼åèªå·±å客ä¹ä½ï¼ææè·¯åé¨åéç¨æ ¸å¿ä»£ç åä¸ä¸å¬å¸ï¼
æ以æ³è¦å®ç°ä¸äºç«é·çåè½è¦ä¸æé«äººå¸®ä½ ï¼
è¦ä¸ä½ å°±åªè½å¥½å¥½å¦ä»£ç ï¼æå¾æä¸æ¥è½å¤å积èåäºã
ææ请èªè¡æ交è¯è®ºéªè¯ã
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
本文实例讲述了php+ ajax 实现的写入数据库操作。分享给大家供大家参考,具体如下:。<input class="tel" type="text" placeholder="请输入您的手机号码&q