• Resolved joxyzan

    (@joxyzhan)


    Howdy!

    I’m a layman trying to put a prewritten comment in the WordPress comment section by modifying comments.php:

    <?php if ( comments_open() ) { comment_form(array(
    	'title_reply'=>'',
    	'label_submit' => esc_html__( 'Send!' ),
    	'comment_field' => esc_html__( 'Prewritten comment' ),
    	)); } ?>

    The two first adjustments works fine… but what should the third one be to fullfil my wish?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    ‘comment_field’ arg should work for you. However, you need to pass the entire HTML for the field, for example:
    'comment_field' => '<textarea id="comment" name="comment" cols="45" rows="5" maxlength="65525" required="">'. __('Prewritten comment','textdomain').'</textarea>',

    Thread Starter joxyzan

    (@joxyzhan)

    Thank you @bcworkz that worked great! ?

    Could you maybe tell me how to echo <?php the_author(); ?> in the same prewritten comment? I can’t figure out where/how to place it to make it show in the comment.

    Moderator bcworkz

    (@bcworkz)

    In my previous example, replace __('Prewritten comment','textdomain') with get_the_author(). This function call would need to be within “the loop” where the current post has certain global values set. It should be the case here since it’s related to comments for a particular post.

    Thread Starter joxyzan

    (@joxyzhan)

    Thanks @bcworkz , what I wanted though is to put the “Prewritten comment” followed by “the author” in the same comment.

    I cant figure out how all punctuation marks (. , ‘ and __) works here… ??

    Moderator bcworkz

    (@bcworkz)

    The __() is a basic translation function on which esc_html__() is based. . dots are concatenation operators for strings, for “gluing” string fragments together. I agree that when all these symbols appear together in a code line it’s difficult to follow what’s going on. You would want to “glue” the translation function and get the author function together with yet another dot ..
    'comment_field' => '<textarea id="comment" name="comment" cols="45" rows="5" maxlength="65525" required="">'. __('Prewritten comment by ','textdomain').get_the_author().'</textarea>',

    'textdomain' is a default value for a required parameter of translation functions. It should normally be the name of your theme or plugin.

    Thread Starter joxyzan

    (@joxyzhan)

    Thanks again @bcworkz , for taking your time to write clear och correct answers! ??

    Thread Starter joxyzan

    (@joxyzhan)

    I’m back @bcworkz ??

    Do you know how to avoid Parse error, unexpected token “echo” when I replace .get_the_author(). with .echo $fetchername->display_name. like this:

    'comment_field' => '<textarea id="comment" name="comment" cols="45" rows="5" maxlength="65525" required="">'. __('Prewritten comment by ','textdomain').echo $fetchername->display_name.'</textarea>',

    $fetchername is a userdata-array taken from a custom field and it works fine for echoing the display_name outside of this weird commentfield-thing i’m making…

    Moderator bcworkz

    (@bcworkz)

    Just remove the echo part. This is not the place or time to generate output with an echo statement. The ‘comment_field’ HTML is being passed to a function which will echo it out at the appropriate time.
    .$fetchername->display_name.

    Thread Starter joxyzan

    (@joxyzhan)

    Ah… I thought I had tried that already but obviously I hadn’t. Thank you for sharing your knowledge. ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Prefill the comment textarea’ is closed to new replies.