6

We're doing some SEO tweaking to a site, and our SEO guru informs us that we need to remove the <h3> tags from the #reply-title element, output from line 1554 of comments-template.php. It's the header text for the comments form.

as seen here :

<?php if ( comments_open( $post_id ) ) : ?>
        <?php do_action( 'comment_form_before' ); ?>
        <div id="respond">
            <h3 id="reply-title"><?php comment_form_title( $args['title_reply'], $args['title_reply_to'] ); ?> <small><?php cancel_comment_reply_link( $args['cancel_reply_link'] ); ?></small></h3>
            <?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) : ?>

We're aware of the numerous filters and hooks associated with the comment_form();, but that <h3> is hard-coded.

for this, we've been unable to come up w/ a sustainable solution to replacing the <h3 id="reply-title"></h3> with just <div id="reply-title"></div>.

it's starting to look like the quickest / easiest option may be to just unhook the call to comment_form(); and hook in a copy of our own function, which would be just a copy, with a simple change to that one line.

But in the meantime, it never hurts to poll the community. Any ideas on how to modify that markup in a sustainable ( non-core hackable ) way?

It should be noted that this can't be solved w/ some CSS or JS. The actual, crawlable DOM has to be dealt with.

thanks again stack.

4
  • 3
    I have nothing of value to add to this, but I do have a question: has the SEO guy explained why this is so important? Seems like a heckuva hoop to jump through, that no one has asked for, or created a plugin to get around. Jul 31, 2013 at 22:51
  • 2
    I am not sure what the SEO value is either. Maybe ask about that part at Webmasters before moving forward on this.
    – s_ha_dum
    Jul 31, 2013 at 22:55
  • Pleas rework your question: proper upper-/lowercase letters, Code MarkUp, etc. (hint: WYSIWYG editor buttons). At least this isn't chat, but a reference for later readers as well. Thanks.
    – kaiser
    Jul 31, 2013 at 23:03
  • Please check the edit for compliance. Is that what you meant? We're new here.
    – AhSo
    Jul 31, 2013 at 23:26

4 Answers 4

9

Today there is a native option to do this without hacking the core, or doing tricky filters with output buffer. You just need to use the filter 'comment_form_defaults' and edit the values from 'title_reply_before' and 'title_reply_after' key:

add_filter( 'comment_form_defaults', 'custom_reply_title' );
function custom_reply_title( $defaults ){
  $defaults['title_reply_before'] = '<span id="reply-title" class="h4 comment-reply-title">';
  $defaults['title_reply_after'] = '</span>';
  return $defaults;
}

In this exemple, i have wrapped the title with a span tag that have no impact on SEO with a class named .h4, with a same style that the original h4 tag has:

h4, .h4 {
/* styles */
}

This way, you can keep the styling from a header without messing up with your SEO. (:

If you are using Bootstrap, this class already exists and are styled the same way i mentioned above to all headers. From H1 to H6 and the respective classes.

1
  • 1
    Surprised this has gotten no love. Well, here's some love! Have an upvote, sir. Feb 22, 2018 at 10:19
3

Had a similar problem, short blog entries found by google etc. showed the comment reply title instead of the blog entry content.

This solution buffers the comment form html and replaces the <h3 id="reply-title"..> with another tag before printing it:

function my_comment_form_before() {
    ob_start();
}
add_action( 'comment_form_before', 'my_comment_form_before' );

function my_comment_form_after() {
    $html = ob_get_clean();
    $html = preg_replace(
        '/<h3 id="reply-title"(.*)>(.*)<\/h3>/',
        '<p id="reply-title"\1>\2</p>',
        $html
    );
    echo $html;
}
add_action( 'comment_form_after', 'my_comment_form_after' );

Note: In older WordPress versions the html was <h3 id="reply-title">, since 3.6 or so it is <h3 id="reply-title" class="comment-reply-title">, both cases are covered in above code.

1
  • wonderful! It works great!
    – Nam Nguyen
    Dec 6, 2018 at 14:49
0

There isn't a way to change that particular <h3> element without hacking core (and you already know not to do that), but you still have the option of creating your own comments form or using a comment plugin that has the markup you want.

If you absolutely, positively don't want to use another method, you could capture the form's HTML output into a ridiculously long string, do a preg_replace on it to change the offending <h3>, then echo that stringified HTML form to the DOM.

0

Not exactly sure which text you are referring to but one of these may help or at least give you some idea:

add_filter( 'comment_author_says_text', 'sp_comment_author_says_text' );
function sp_comment_author_says_text() {
    return 'author says';
}


add_filter( 'comment_form_defaults', 'sp_comment_form_defaults' );
function sp_comment_form_defaults( $defaults ) {

    $defaults['title_reply'] = __( 'Leave a Comment' );
    return $defaults;

}

Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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