Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wordpress comment AJAX send
<?php
namespace zdrojak\comments;
/**
* Odeslani komentaru pomoci Ajaxu
*
* @author Roman Ozana <ozana@omdesign.cz>
*/
class AjaxComnment {
public static function init() {
return new self;
}
public function __construct() {
add_action('comment_post', array(&$this, 'ajaxComment'), 999, 2);
add_action('comment_form_top', array(&$this, 'addStatusDivBefore'));
add_action('init', array(&$this, 'detectCommentAjaxRequest'));
}
public function detectCommentAjaxRequest() {
global $pagenow;
if (
$pagenow === 'wp-comments-post.php' &&
!defined('DOING_AJAX') &&
isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'
) {
@header('Content-Type: text/html; charset=' . get_option('blog_charset'));
@header('X-Robots-Tag: noindex');
send_nosniff_header();
nocache_headers();
define('DOING_AJAX', true);
}
}
public function ajaxComment($comment_ID, $comment_status) {
if (DOING_AJAX) {
wp_send_json(
array(
'status' => 'success',
'message' => 'Děkujeme za Váš komentář.',
'comment' => $comment_status,
)
);
}
}
public function addStatusDivBefore() {
echo '<div id="comment-status"></div>';
}
}
.alert {
padding: 8px 35px 8px 14px;
margin: 5px;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
background-color: #fcf8e3;
border: 1px solid #fbeed5;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.alert-danger, .alert-error {
color: #b94a48;
background-color: #f2dede;
border-color: #eed3d7;
}
.alert-info {
color: #3a87ad;
background-color: #d9edf7;
border-color: #bce8f1;
}
.alert-success {
color: #468847;
background-color: #dff0d8;
border-color: #d6e9c6;
}
.spinner {
display: block;
background-image: url('data:image/gif;base64,R0lGODlhQwAKAOMAAMzOzOzu7Nze3NTW1Pz6/OTm5NTS1PT29OTi5Nza3Pz+/P///wAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQJBgALACwAAAAAQwAKAAAEvHDJSau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j5ISDAuPEZePi5uQnkKVAXoSDgG4CKdVgaqGIgdeYCNjZCMJ0BEHBSNpaXAiyd0+XdnEfGQRo4CChM4STAlsR9tCTPQ4Ozl1Sj478SJPokT5cwXLlUMwEiqM0aAhjQO1FkqcSCIEACH5BAkGABAALAAAAABDAAoAhKSmpNTW1Ozu7MzOzOTi5Pz6/LSytNze3PT29Ly6vNza3PTy9NTS1OTm5Pz+/LS2tP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAXLICSOZGmeaKqubOu+cFEghTPKsy06Mk32tRvwNwuKcAjdAmBoNkcDhSIgGB2m0tGCEVAMdJDutDFqiAMjR1TBQIywioNI8BjYB4l3o0GoiggNAg1yIggKgQEFentkf4wKaQF7BwtWjARzCQMMdnkikgIHfhCApVoHewqKIqgCgGWBDZBHh4OVrIKucw+cnJ4QDFIMo1dSsxBbUgOrYcaYIrJSaEdrbW/Sz3R3eDc0SUII3zvezBBF4uZF5epg50pMTgYw8/T19vf4IiEAOw==');
background-repeat: no-repeat;
background-position: 50% 50%;
min-width: 85px;
min-height: 16px;
}
/**
* @author Roman Ozana <ozana@omdesign.cz>
*/
(function ($) {
$(document).ready(function () {
$('#commentform').bind('submit', function (event) {
var status = $('#comment-status');
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
data: $(this).serialize(),
error: function (XMLHttpRequest, textStatus, errorThrown) {
status.addClass('alert alert-error').text(errorThrown);
},
beforeSend: function () {
status.removeClass().addClass('spinner').show();
},
success: function (data, textStatus) {
if (data.status == 'success') {
console.log(data)
status.addClass('alert alert-success').text(data.message);
$('#comment').val('');
$('#title').val('');
} else {
status.addClass('alert alert-error').html(data);
}
},
complete: function () {
status.delay(5000).slideUp('slow');
status.removeClass('spinner');
}
});
event.preventDefault();
}
);
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.