2

Anyone know how can I change the comment reply text in wordpress to only reflect on specific pages only and not the whole site?

For example the default text site-wide is “Leave a Reply”. But for some specific pages only, I want the text to read “Leave a Review”.

Is there some code I could use in the comments.php file that would make this work?

2 Answers 2

2

Code is not tested. But theoretically it should work:

add_filter('comment_form_defaults', 'wpse337366_comment_form_modification');
function wpse337366_comment_form_modification($defaults)
{
    // put your condition here
    if( is_page('my-page') || is_singular('my_post_type') )
    {
        $defaults['title_reply'] = __('Leave a Review');
    }
    return $defaults;
}

You can place the code in a plugin or your theme's functions.php.

The code will filter the default texts passed to the comment_form().

Reference

3
  • Thanks, I will try it. But how will this code know which pages I want the new text to show and which pages I want the default text to show? Commented May 9, 2019 at 7:13
  • @dudesonwill pages are filtered in here if( is_page('my-page')
    – Vishwa
    Commented May 9, 2019 at 7:19
  • @dudesonwill It this one solve your problem, you can mark the answer as 'accepted'. By this way, any one searching for the same issue can be helped. Commented May 21, 2019 at 7:55
0

In your page template, when you call comments template like this, <?php comment_form(); ?> it'll load default theme template. In the page template you need to change comment title, simply call comments template like below,

<?php
      comment_form(array(
      'title_reply' => __( 'Leave a Review' ),
 ));?>
4
  • But wouldn't this change the text for all of the pages and not just the few specific pages I want it to be on only? Commented May 9, 2019 at 7:14
  • No. This actually for few specific pages. use a separate page template for each page you need this to be there. it can be identical to your other pages templete but only change is above section. let me know if you need any help with page templates
    – Vishwa
    Commented May 9, 2019 at 7:18
  • I was able to create a separate page template but when trying to use the code you provided I am not sure where to place it in the template correctly. You said when the page calls comment's template it would look like this <?php comment_form(); ?> but it seems on my template it is different. This is what it looks like on my template below: <?php if ( comments_open() || get_comments_number() ) : echo '<section style="padding: 0px 15px 15px;">'; comments_template( '' , true ); echo '</section>'; endif; ?> In this case, how would I use your code here correctly? Commented May 11, 2019 at 9:38
  • @dudesonwill try replacing <?php if ( comments_open() || get_comments_number() ) : echo '<section style="padding: 0px 15px 15px;">'; comments_template( '' , true ); echo '</section>'; endif; ?>with <?php comment_form(); ?> Your actual comments template is loaded from different file named (usually) comments.php
    – Vishwa
    Commented May 13, 2019 at 4:44

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.