1

I have added a custom subscribe field to the WooCommerce comment form. However, right now the field is above everything else. I am not sure how to move it to the bottom below the other fields.

The following code adds the field above everything when user is logged in or logged out.

add_filter( 'comment_form_fields', function( $fields ) {
    echo '<div class="checkbox-wrapper"><input type="checkbox" id="subscribe" tabindex="0" class="hidden"><label for="subscribe" class="visible checkbox">Subscribe to our E-Newsletter</label></div>';
    return $fields;
} );

logged in formlogged out form

I first tried the code below, where it adds the subscribe checkbox at the right position for logged out users. However, the subscribe button is above everything else for the logged in user.

add_action( 'comment_form_logged_in_after', 'additional_fields' );
add_action( 'comment_form_after_fields', 'additional_fields' );
function additional_fields () {
    echo '<div class="checkbox-wrapper"><input type="checkbox" id="subscribe" tabindex="0" class="hidden"><label for="subscribe" class="visible checkbox">Subscribe to our E-Newsletter</label></div>';
}

I have also looked at the following page for possible hook positions but it doesn't seem like it's possible to move the field to just above the submit button.

http://wpengineer.com/2205/comment-form-hooks-visualized/

Any thoughts? Is JavaScript the only option?

2 Answers 2

0

You should return the div element as a variable, like

$subscribe = '<div>...</div>'; return $subscribe;

By doing echo function, the output is sometimes on top.

0

The issue causing this seems to originate from WooCommerce as it appears correctly in the blog post but looks different in WooCommerce reviews. There are two different ways to solve this. It's up to you which one you prefer.

way 1:

function vono_fields_html()
{
  ob_start();
?>

  <p><label for="hide_name"> <input type="checkbox" value="1" name="hide_name" id="hide_name" /> <?= __('İsim ve soyismi gizle', 'woocommerce') ?></label></p>

<?php
  return ob_get_clean();
}


add_filter('comment_form_field_comment', 'render_vono_fields', 99, 1);
function render_vono_fields($comment_field)
{
  if (!is_product()) {
    return $comment_field;
  }
  return $comment_field . vono_fields_html();
}

enter image description here

way 2:

This approach applies to all types of comments.

add_filter('comment_form_field_comment', 'vono_form_field');
function vono_form_field($field)
{
  $field .= "www.gencyazilimci.com";
  return $field;
}

enter image description here

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.