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.

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 );
    }
} );
  • 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 Nov 5 '15 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 Nov 5 '15 at 18:34
  • Damn! that means no one knows when this is gonna fix. Damn! – iSaumya Nov 5 '15 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 Nov 5 '15 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 );
  • 1
    Use printf and sprintf to translate strings with HTML and then use placeholders to replace HTML tags – Pieter Goosen Nov 5 '15 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 Nov 5 '15 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 Nov 6 '15 at 0:17

protected by Community Feb 21 '17 at 8:37

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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