Skip to content

Instantly share code, notes, and snippets.

@isaumya
Created November 19, 2015 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaumya/838fc43afb758d62e2fe to your computer and use it in GitHub Desktop.
Save isaumya/838fc43afb758d62e2fe to your computer and use it in GitHub Desktop.
Show Back to Page Link in WordPress Blank Comment Posting
<?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