Success message in comment form

Without Ajax or plugins. Add to function.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, "https://wordpress.stackexchange.com/" );
        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 );

Leave a Comment