备注表单中的成功消息

时间:2014-03-22 作者:marsweb

我正在使用wordpress 3.8。我正在使用comment_form(); 我需要显示success message 上成功在博客上发布评论。如何做到这一点?

2 个回复
SO网友:wp-mario.ru

没有Ajax或插件。添加到函数。php:

add_action( \'set_comment_cookies\', function( $comment, $user ) {
    setcookie( \'ta_comment_wait_approval\', \'1\' );
}, 10, 2 );

add_action( \'init\', function() {
    if( $_COOKIE[\'ta_comment_wait_approval\'] === \'1\' ) {
        setcookie( \'ta_comment_wait_approval\', null, time() - 3600, \'/\' );
        add_action( \'comment_form_before\', function() {
            echo "<p id=\'wait_approval\' style=\'padding-top: 40px;\'><strong>Your comment has been sent successfully.</strong></p>";
        });
    }
});

add_filter( \'comment_post_redirect\', function( $location, $comment ) {
    $location = get_permalink( $comment->comment_post_ID ) . \'#wait_approval\';
    return $location;
}, 10, 2 );

SO网友:item251

尝试以下操作:http://wordpress.org/plugins/wp-ajaxify-comments/

或(手动)

将以下代码行添加到主题的函数中。php文件

    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;  
    }  
}  
您需要添加两个JavaScript文件jquery。验证min.js和ajax注释。js位于主题的js目录中。http://bassistance.de/jquery-plugins/jquery-plugin-validation/

以及ajax注释。js是:

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(\'\');  
                }  
            });  
        }  
    });  
});  
自定义消息的样式

.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;   
} 
打开主题的注释。php并向注释表单输入字段添加一些CSS类,如下所述:要注释作者姓名输入,请添加class=“required”以注释作者电子邮件输入,添加class=“required email”以注释作者URL输入,将class=“URL”添加到注释文本区,添加class=“required”

结束

相关推荐

COMMENTS_POPUP_LINK()-如何根据不同的评论条件加载不同的类/图片?

作为Codex 表示语法:<?php comments_popup_link( $zero, $one, $more, $css_class, $none ); ?> 很明显,我们可以根据不同的注释条件加载不同的文本,如“注释打开”、“1注释”、“2+注释”和“注释关闭”,此外,我们还可以将CSS类加载到该部分。我想从具有不同注释条件的图像精灵加载不同的图像:评论打开但未发布时-发布评论时加载一条-关闭评论时加载一条-加载一条What I thought。。。就是加载不同的类,这样我