0

When you visit any wordpress post and click on the "Post Comment" button it just post that blank data to the server and while doing the check at php level it shows this annoying error message to users:

enter image description here

we all have seen it. but what I was wondering is if there is a way to add a "Back to XYZ Article" link in there. Where XYZ Artcile is the post page he way visiting while he accidentally clicked that post comment button.

It happens to many people many times.

Looking forward to your ideas.

2 Answers 2

1

I think 'pre_comment_on_post' is a better hook to use, although you'll have to reproduce some logic from "wp-comments-post.php" to use it, eg:

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 );
    }
} );
4
  • Thank you very much for the snippet. It works fantastically. I've reported a comment bug in wordpress months back but no one said anything. Can you take a look - core.trac.wordpress.org/ticket/34110 ?
    – iSaumya
    Commented Nov 5, 2015 at 17:41
  • Yeah unfortunately 5 weeks is like 5 seconds in WP trac time. There's outstanding bugs 5 years or more old, and bug reports are often met with endless silence. There's been a bug drive for 4.4 which has got the bug count down to about 3,200 (!) so maybe things will improve...but don't hold your breath!
    – bonger
    Commented Nov 5, 2015 at 18:34
  • Damn! that means no one knows when this is gonna fix. Damn!
    – iSaumya
    Commented Nov 5, 2015 at 18:46
  • By the way, the function you provided here. This should be the part of wp core. I don't know why wp dev give so less importance to comments.
    – iSaumya
    Commented Nov 5, 2015 at 20:40
1

I don't see any hook there, but you could use the gettext filter hook. Adding the following function to your theme's functions.php should do the trick. Replace text-domain with your theme's text domain.

if ( ! function_exists( 'my_gettext' ) ) {
    function my_gettext( $translated_text, $untranslated_text, $domain ) {
        global $pagenow;

        if ( $pagenow === 'wp-comments-post.php' ) {
            switch ( $untranslated_text ) {
                case '<strong>ERROR</strong>: please fill the required fields (name, email).' :
                    $translated_text = __( '<strong>ERROR</strong>: please fill the required fields (name, email).<br /><a href="javascript:javascript:history.go(-1)">Back to XYZ Article</a>', 'text-domain' );
                    break;
            }
        }
        return $translated_text;
    }
}
add_filter( 'gettext', 'my_gettext', 20, 3 );

The function looks for the case in wp-comments-post.php and replaces it with the $translated_text.

The downside is that we would normally not include HTML in internationalization functions, yet splitting it doesn't work. So, the following would throw an error:

$translated_text = '<strong>' . __( 'ERROR', 'text-domain' ) . '</strong>:' . __( 'please fill the required fields (name, email).', 'text-domain' ) . '<br /><a href="javascript:javascript:history.go(-1)">' . __( 'Back to XYZ Article', 'text-domain' ) . '</a>';


UPDATE following Pieter's comment

Just for the record, with sprinf it is indeed not necessary to include HTML in internationalization functions...

if ( ! function_exists( 'my_gettext' ) ) {
    function my_gettext( $translated_text, $untranslated_text, $domain ) {
        global $pagenow;
        if ( $pagenow === 'wp-comments-post.php' ) {
            switch ( $untranslated_text ) {
                case '<strong>ERROR</strong>: please fill the required fields (name, email).' :
                    $translated_text = sprintf( __( '%1$s: please fill the required fields (name, email).%2$s', 'text-domain' ), '<strong>' . __( 'ERROR', 'text-domain' ) . '</strong>', '<br /><a href="javascript:javascript:history.go(-1)">' . __( 'Back to XYZ Article', 'text-domain' ) . '</a>' );
                    break;
            }
        }
        return $translated_text;
    }
}
add_filter( 'gettext', 'my_gettext', 20, 3 );
3
  • 1
    Use printf and sprintf to translate strings with HTML and then use placeholders to replace HTML tags Commented Nov 5, 2015 at 5:02
  • Thank you for your code. I've posted a bug report in wordpress bug report tracker months back related to comment. Weirdly no one said anything -core.trac.wordpress.org/ticket/34110 Can yo take a look?
    – iSaumya
    Commented Nov 5, 2015 at 17:41
  • Pieter, that makes sense & good to know. TY! iSaumya, sorry, I'm still too much of a noob to get involved in WP core. :)
    – Axel
    Commented Nov 6, 2015 at 0:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.