user2966586 user2966586 - 3 months ago 24
PHP Question

Undefined variable on comments wordpress

Im tidying up my custom theme and in debug mode I am getting a undefined variable in my comments section. This is my custom comment section and the undefined variables showing up are

aria_req
,
req
and
commenter
. This custom comment function is within my functions file. How would I fix this?

// Edit Comment Forms
function alter_comment_form_fields($fields){

$fields['author'] = '<div class="row"><div class="col-md-4"><p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
'<input id="author" name="author" type="text" placeholder="Name" value="' . esc_attr( $commenter['comment_author'] ) . '"' . $aria_req . ' /></p></div>';
//Edit Email Field
$fields['email'] = '<div class="col-md-4"><p class="comment-form-author">' . '<label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
'<input id="email" name="email" type="text" placeholder="Email" value="' . esc_attr( $commenter['comment_author_url'] ) . '" "' . $aria_req . ' /></p></div>';
//Edit Website Field
$fields['url'] = '<div class="col-md-4"><p class="comment-form-author">' . '<label for="url">' . __( 'Website' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
'<input id="url" name="url" type="text" placeholder="Website" value="' . esc_attr( $commenter['comment_author_url'] ) . '"' . $aria_req . ' /></p></div></div>';

return $fields;
}

add_filter('comment_form_default_fields','alter_comment_form_fields');

Answer

You can refer to http://codex.wordpress.org/Function_Reference/comment_form
The variables can be defined above your code (in the beginning of the function body) in the following manner:

$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
Comments