0
function modify_comment_form_fields($fields){
  // Check if $fields is an array
  if (!is_array($fields)) {
      return $fields;
  }

  ob_start();
  $submit_button = ob_get_clean();
  $submit_button .= '<input name="submit" type="submit" id="submit" class="submit" value="Post Comment">';
  // Check if $submit_button is a string
  if (!is_string($submit_button)) {
      return $fields;
  }

  $fields['submit_field'] = $submit_button;
  return $fields;
}
add_filter('comment_form_submit_field', 'modify_comment_form_fields');

The submit button is wrapped under the p tag inside a WordPress comment form.

I wanted to get rid of this → <p class="form-submit"></p>

But the solution didn't work, Can someone help me to take this further in troubleshooting?

Currently, In browser the output of submit button is rendered as this one →

<p class="form-submit"><input name="submit" type="submit" id="submit" class="submit" value="Post Comment"> <input type="hidden" name="comment_post_ID" value="8" id="comment_post_ID">
<input type="hidden" name="comment_parent" id="comment_parent" value="0">
</p>

Update The comment form looks like this →

    $fields = array(
  'heading_tag' => '<h6>Leave an opinion</h6>',
  'comment_field' => '<textarea name="comment" id="comment" cols="30" rows="6" placeholder="Your opinion matters, but do not spam us!" required></textarea>',
  'author' => '<input type="text" name="author" placeholder="Enter Full Name" value="' . esc_attr( $commenter['comment_author'] ) . '" required>',
  'email'  => '<input type="text" name="email" placeholder="Enter Email ID" value="' . esc_attr( $commenter['comment_author_email'] ) . '" required>'
  
);

$comment_args = array(
  'title_reply'        => '',
  'fields'             => $fields,
  'comment_field'      => $fields['comment_field'],
  'class_submit'       => 'submit',
  'label_submit'       => 'Post Comment',
  'class_form'         => 'comform'
);


comment_form($comment_args);
4
  • That looks OK to me, except for the fields-as-an-array check at the beginning. It won't be. What's that for? The comment_form_submit_field hook accepts a string (plus the comment form args) and should return a string too, which you're not doing.
    – Rup
    Mar 16 at 16:58
  • Also the ob_start(), ob_get_clean() is useless, since you're not echoing anything between the two statements.
    – Rup
    Mar 16 at 17:00
  • I assume you control the theme, though? It would be simpler to just pass in the submit_field argument to comment_form() rather than trying to filter it afterwards.
    – Rup
    Mar 16 at 17:01
  • Update the snipped also gave comment form structure.
    – WordCent
    Mar 16 at 17:06

1 Answer 1

2

If you control the theme the simplest way to fix this is passing the submit_field argument into comment_form():

comment_form(
    array(
        'submit_field' => '%1$s %2$s',
    )
);

Default: <p class="form-submit">%1$s %2$s</p>, where %1$s is the submit button markup and %2$s is the comment hidden fields.

which removes the <p> altogether. However it might be useful to wrap the button and hidden fields in a div instead, e.g. 'submit_field' => '<div class="form-submit">%1$s %2$s</div>'.

As above the problem with your code is you've got the filter input and output arguments wrong: comment_form_submit_field has inputs $submit_field (string), $args (array), and should return a string.

2
  • I was wondering with complicated solution, It was so nice, crisp and simple solution form you. Thanks.
    – WordCent
    Mar 16 at 17:07
  • Can you explain this in more detail → As above the problem with your code is you've got the filter input and output arguments wrong: comment_form_submit_field has inputs $submit_field (string), $args (array), and should return a string.
    – WordCent
    Mar 16 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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