0

I am new to wordpress theme development and I am trying to create a custom comment form. I read the documentation and I have a reach a point that I am happy with the layout but I have 2 issues with form's functionality.

First issue is when I am logged in to wordpress when I hit the "Post Comment" button, the message is not saved in MySQL. I believe that I am not doing something correctly while retrieving user input.

My second issue is that when I am logged out, I have some fields name and email which are not saved also for a reason and I face the below error: Error: Please fill the required fields (name, email).

Attached you will find the code that I used to create the custom comment form. The function is called from single.php file which is the file that I created to display a single post.

Please note that I am asking your help not to resolve it for me, but just to learn and identify what I am doing wrong. Excuse my bad english. I am Greek!

function dck_custom_comment_form() {
//Declare Vars
$comment_author = 'Name *';
$comment_email = 'Email *';
$comment_body = 'Comment *';
$comment_cookies_1 = ' By commenting you accept the';
$comment_cookies_2 = ' Privacy Policy';
$commenter = wp_get_current_commenter();

$fields = array(
    //Author field
    'author' => '<div class="row"><div class="col-md-6"><div class="form-group"><input name="Name" id="name" placeholder="' . $comment_author .'" class="form-control" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '"></div></div>',
    //Email Field
    'email' => '<div class="col-md-6"><div class="form-group"><input name="Email" id="email" placeholder="' . $comment_email .'" class="form-control" type="email" value="' . esc_attr(  $commenter['comment_author_email'] ) . '"></div></div></div>',
    //Cookies
    'cookies' => '<div class="col-md-12"><div class="form-group"><input type="checkbox" required>' . $comment_cookies_1 . '<a href="' . get_privacy_policy_url() . '">' . $comment_cookies_2 . '</a></div></div>',
);

//Array
$comments_args = array(
    'fields' => $fields,
    'comment_field' => '<div class="row"><div class="col-md-12"><div class="form-group"><textarea name="message" id="message" placeholder="' . $comment_body .'" rows="4" aria-required="true" class="form-control"></textarea></div></div></div>',
    'comment_notes_after' => '',
    'title_reply' => 'Please Post Your Comments & Reviews',
    'label_submit' => 'Post Comment'
);
comment_form( $comments_args );}


function dck_reorder_comment_form_fields( $fields ) {
$comment_field = $fields['comment'];
$cookies = $fields['cookies'];
unset( $fields['comment'] );
unset( $fields['cookies'] );
$fields['comment'] = $comment_field;
$fields['cookies'] = $cookies;
return $fields;
}
0

I resolved the case. Problem here was on &fields variable declaration and also to 'comment_field' assignment. I had to change name fields with the correct ones.

Check code below:

function dck_custom_comment_form() {
   //Declare Vars
   $comment_author = 'Name *';
   $comment_email = 'Email *';
   $comment_body = 'Comment *';
   $comment_cookies_1 = ' By commenting you accept the';
   $comment_cookies_2 = ' Privacy Policy';
   $commenter = wp_get_current_commenter();

$fields = array(
    //Author field
    'author' => '<div class="row"><div class="col-md-6"><div class="form-group"><input name="author" id="name" placeholder="' . $comment_author .'" class="form-control" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '"></div></div>',
    //Email Field
    'email' => '<div class="col-md-6"><div class="form-group"><input name="email" id="email" placeholder="' . $comment_email .'" class="form-control" type="email" value="' . esc_attr(  $commenter['comment_author_email'] ) . '"></div></div></div>',
    //Cookies
    'cookies' => '<div class="col-md-12"><div class="form-group"><input type="checkbox" required>' . $comment_cookies_1 . '<a href="' . get_privacy_policy_url() . '">' . $comment_cookies_2 . '</a></div></div>',
);

//Array
$comments_args = array(
    'fields' => $fields,
    'comment_field' => '<div class="row"><div class="col-md-12"><div class="form-group"><textarea name="comment" id="message" placeholder="' . $comment_body .'" rows="4" aria-required="true" class="form-control"></textarea></div></div></div>',
    'comment_notes_after' => '',
    'title_reply' => 'Please Post Your Comments & Reviews',
    'label_submit' => 'Post Comment'
);
comment_form( $comments_args );
  }

Thanks for your effort!

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.