Created
November 19, 2015 09:38
-
-
Save isaumya/838fc43afb758d62e2fe to your computer and use it in GitHub Desktop.
Show Back to Page Link in WordPress Blank Comment Posting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'pre_comment_on_post', function ( $comment_post_ID ) { | |
$go_back = sprintf( __( '<br><a href="javascript:history.go(-1)">Back to "%s" Article</a>' ), get_the_title( $comment_post_ID ) ); | |
// Part-copied from "wp-comments-post.php", with $go_back tagged onto error strings. | |
$comment_author = ( isset($_POST['author']) ) ? trim(strip_tags($_POST['author'])) : null; | |
$comment_author_email = ( isset($_POST['email']) ) ? trim($_POST['email']) : null; | |
$comment_content = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null; | |
$user = wp_get_current_user(); | |
if ( get_option('require_name_email') && !$user->exists() ) { | |
if ( 6 > strlen( $comment_author_email ) || '' == $comment_author ) { | |
wp_die( __( '<strong>ERROR</strong>: please fill the required fields (name, email).' ) . $go_back, 200 ); | |
} elseif ( ! is_email( $comment_author_email ) ) { | |
wp_die( __( '<strong>ERROR</strong>: please enter a valid email address.' ) . $go_back, 200 ); | |
} | |
} | |
if ( '' == $comment_content ) { | |
wp_die( __( '<strong>ERROR</strong>: please type a comment.' ) . $go_back, 200 ); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment