Search code examples
phpwordpressrenderingrecaptcha

Change positionining of captcha in comment section


I would like to change the order in which the comment elements are being rendered. I am using the 'Advanced noCaptcha & invisible captcha' plugin from wordpress. The captcha is rendered inbetween the inputfields and the textare field (see picture). I want it to be rendered in beween the textarea field and the submit button.
Any Ideas on how to achive this?
Cheers.

Here is what I found in the comments.php:

$fields = [];   
$fields['author']  = '<label id="comment-input"><input /*some input stuff*/ placeholder="NAME*"/>';
$fields['email']   = '<input /*some input stuff*/ placeholder="IHRE E-MAIL ADRESSE * (wird nicht angezeigt)"/>';
$fields['url']     = '<input /*some input stuff*/ placeholder="Webseite" />';



$comments_args = [
    'fields'               => apply_filters( 'comment_form_default_fields', $fields ),
    'comment_field'        => '<div id="comment-textarea"><label class="screen-reader-text" for="comment">' . esc_attr__( 'Comment', 'Avada' ) . '</label><textarea  /*some input stuff*/" placeholder="KOMMENTAR VERFASSEN..."></textarea></div>',
    'title_reply'          => esc_html__( 'Leave A Comment', 'Avada' ),
    'title_reply_to'       => esc_html__( 'Leave A Comment', 'Avada' ),
    //some more comment args
];
comment_form( $comments_args );

enter image description here


Solution

  • I came up with a workarround. To give some context: The captcha appears to be appended to the last element inside the comment section, while ignoring the textarea comment box.
    there is a WP-filter that can be used to rearrange the different fields. I arranged the fields according to my needs and then added an additional blank field at the end (placeholder), for the captcha to append to it. This seems to do the trick. Without the placeholder, even if the 'comment' field is last, it doesnt work.
    Here is the code I used in the functions.php:

    function change_comment_fields_order($fields)
    {
      $comment_field = $fields['comment'];
      $author_field = $fields['author'];
      $email_field = $fields['email'];
      $url_field = $fields['url'];
      //unset
      unset($fields['comment']);
      unset($fields['author']);
      unset($fields['email']);
      unset($fields['url']);
      // the order of fields is the order below, change it as needed:
      $fields['author'] = $author_field;
      $fields['email'] = $email_field;
      $fields['url'] = $url_field;
      $fields['comment'] = $comment_field;
      //placeholder is a little hack that prevents the recaptcha thingy from being rendered in between the comment and the url fileds
      $fields['placeholder'] = "";
      // done ordering, now return the fields:
      return $fields;
    }
    add_filter('comment_form_fields', 'change_comment_fields_order');