Komunikat o powodzeniu w formie komentarza
![Komunikat o powodzeniu w formie komentarza](https://oen.pl/wp-content/uploads/2023/09/apple-touch-icon@2.png)
Spróbuj tego:
lub (ręcznie)
Dodaj następujące linie kodu do plikufunctions.php motywu
add_action('init', 'wdp_ajaxcomments_load_js', 10);
function wdp_ajaxcomments_load_js(){
wp_enqueue_script('ajaxValidate', get_stylesheet_directory_uri().'/wdp-ajaxed-comments/js/jquery.validate.min.js', array('jquery'), '1.5.5');
wp_enqueue_script('ajaxcomments', get_stylesheet_directory_uri().'/wdp-ajaxed-comments/js/ajax-comments.js', array('jquery', 'ajaxValidate'), '1.1');
}
add_action('comment_post', 'wdp_ajaxcomments_stop_for_ajax',20, 2);
function wdp_ajaxcomments_stop_for_ajax($comment_ID, $comment_status){
if(!emptyempty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
//If AJAX Request Then
switch($comment_status){
case '0':
//notify moderator of unapproved comment
wp_notify_moderator($comment_ID);
case '1': //Approved comment
echo "success";
$commentdata=&get_comment($comment_ID, ARRAY_A);
$post=&get_post($commentdata['comment_post_ID']); //Notify post author of comment
if ( get_option('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] )
wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
break;
default:
echo "error";
}
exit;
}
}
Będziesz musiał dodać dwa pliki JavaScript jquery.validate.min.js i ajax-comments.js w katalogu js swojego motywu.
a plik ajax-comments.js to:
jQuery('document').ready(function($){
var commentform=$('form[action$=wp-comments-post.php]');
commentform.prepend('<div id="wdpajax-info" ></div>');
var infodiv=$('#wdpajax-info');
commentform.validate({
submitHandler: function(form){
//serialize and store form data in a variable
var formdata=commentform.serialize();
//Add a status message
infodiv.html('<p>Processing...</p>');
//Extract action URL from commentform
var formurl=commentform.attr('action');
//Post Form with data
$.ajax({
type: 'post',
url: formurl,
data: formdata,
error: function(XMLHttpRequest, textStatus, errorThrown){
infodiv.html('<p class="wdpajax-error" >You might have left one of the fields blank.</p>');
},
success: function(data, textStatus){
if(data=="success")
infodiv.html('<p class="wdpajax-success" >Thanks for your comment. We appreciate your response.</p>');
else
infodiv.html('<p class="wdpajax-error" >Error in processing your form.</p>');
commentform.find('textarea[name=comment]').val('');
}
});
}
});
});
Dostosuj styl wiadomości
.wdpajax-error{
border:1px solid #f9d9c9;
padding:5px;
color:#ff3311;
}
.wdpajax-success{
border:1px solid #339933;
padding:5px;
color:#339933;
}
label.error{
float:none !important;
padding-left:5px;
color:#ff3311;
}
Otwórz plik Comments.php swojego motywu i dodaj kilka klas CSS do komentowania pól wejściowych zgodnie z opisem: Aby skomentować wprowadzone nazwisko autora, dodaj class=”wymagane” Aby skomentować wprowadzony adres e-mail autora, dodaj class=”wymagany adres e-mail” Aby skomentować wprowadzony adres URL autora, dodaj klasę=”url” Do pola tekstowego komentarza dodaj klasę=”wymagane”