WordPress comment form customization

wordpressCopy and Paste following into your functions.php file and see the customized form
<?php
function po_custom_comment_fields($fields)
{
//define global variable
global $post;

//remove defaults fields here
$fields[‘url’] = ”;

//begin custom fields
$fields[‘firstname’] = ” . ‘<p class=”comment-form-firstname”><label for=”firstname”>’ . __( ‘First Name’ ) . ‘<span class=”required”>*</span></label>’ .
‘<input id=”firstname” name=”firstname” type=”text” value=”‘ . esc_attr( $commenter[‘firstname’] ) . ‘” size=”30″ aria-required=”true” /></p>’;
$fields[‘lastname’] = ” . ‘<p class=”comment-form-lastname”><label for=”lastname”>’ . __( ‘Last Name’ ) . ‘</label>’ .
‘<input id=”lastname” name=”lastname” type=”text” value=”‘ . esc_attr( $commenter[‘lastname’] ) . ‘” size=”30″ /></p>’;
$fields[’email’] = ” . ‘<p class=”comment-form-email”><label for=”email”>’ . __( ‘Email’ ) . ‘<span class=”required”>*</span></label>’ .
‘<input id=”email” name=”email” type=”text” value=”‘ . esc_attr( $commenter[’email’] ) . ‘” size=”30″ aria-required=”true” /></p>’;
$fields[‘phoneno’] = ” . ‘<p class=”comment-form-phoneno”><label for=”phoneno”>’ . __( ‘Phone No.’ ) . ‘</label> ‘ .
‘<input id=”phoneno” name=”phoneno” type=”text” value=”‘ . esc_attr( $commenter[‘phoneno’] ) . ‘” size=”30″ /></p>’;

//add all the above fields into array and return the array
$fields = array($fields[‘firstname’], $fields[‘lastname’], $fields[‘phoneno’], $fields[’email’] );
return $fields;
}

add_filter( ‘comment_form_default_fields’, ‘po_custom_comment_fields’ );

add_action (‘comment_post’, ‘add_meta_settings’, 1);
function add_meta_settings($comment_id) {
add_comment_meta($comment_id, ‘firstname’, $_POST[‘firstname’], true);
add_comment_meta($comment_id, ‘lastname’, $_POST[‘lastname’], true);
add_comment_meta($comment_id, ‘phoneno’, $_POST[‘phoneno’], true);
}
?>

Like to share it

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *