Copy paste is hurting your theme

I’ve seen quite a few themes that have been doing this thing of passing an argument to the function when they could be using a filter. I get it we all have our methods. But when there is a better practice would you rather use the better practice or a wrong one?

I choose the better practice. Any day. The last several themes I’ve encountered have been using a snippet that has driven me up a wall. A very unhappy wall. The following code appears to be used a quite a few themes and it needs to stop. Like quickly.

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

$comments_args = array(
	// change the title of send button
	// 'label_submit'=>'Send',
	// change the title of the reply section
	// 'title_reply'=>'Write a Reply or Comment',
	// remove "Text or HTML to be displayed after the set of comment fields"
	'comment_notes_after' => ' ',
	// redefine your own textarea (the comment body)
	'comment_field' => '<p class="comment-form-comment"><textarea id="comment" name="comment" aria-required="true" placeholder="'. __('Your Message...', 'text-domain') .'" rows="8" cols="37" wrap="hard"></textarea></p>',
	'fields' => apply_filters( 'comment_form_default_fields', array(
		'author' => '<div class="row"><div class="col-md-4"><p class="comment-form-author">' . '<input id="author" placeholder="'. __('Your name *....', 'text-domain') .'" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' />' . ( $req ? '<span style="color:red" class="required"></span>' : '' ) . '</p></div>',
		'email' => '<div class="col-md-4"><p class="comment-form-email">' . '<input id="email" placeholder="'. __('Your email *...','text-domain') .'" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' />' . ( $req ? '<span style="color:red" class="required"></span>' : '' ) . '</p></div>',
		'url' => '<div class="col-md-4"><p class="comment-form-url">' . '<input id="url" placeholder="'.  __('Your website...', 'text-domain') .'" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p></div></div>'
		) ),
);

comment_form($comments_args); ?>

The biggest reason is that you can use a filter for all of that. Not only is it cleaner it is much, much child-theme friendlier as well. Actually there are two filters that you can use as well! The first one is the comment_form_default_fields. The second is the comment_form_defaults. Each one returns an associative array.

Let’s look at the first one:

Default Fields

The default fields filter is super simple and you can add, remove and modify the input fields for the comment form. I can’t think of a major reason to want to add to that in a theme but that is a possibility. Let’s see a quick example of just removing the wrapping p tags:

add_filter( 'comment_form_default_fields', 'jmc_commentd_fields' );
function jmc_commentd_fields( $fields ) {

	// get the current commenter if available
	$commenter = wp_get_current_commenter();

	// core functionality
	$req      = get_option( 'require_name_email' );
	$aria_req = ( $req ? " aria-required='true'" : '' );
	$html_req = ( $req ? " required='required'" : '' );

	// Change just the author field
	$fields['author'] = '<label for="author">' . __( 'Name:', 'text-domain' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label><input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" maxlength="245"' . $aria_req . $html_req . ' />';
	return $fields;

}

For those fields, there are three that are available:

  • name
  • email
  • url

Do make sure that that you are somehow showing that the site owner does or does not require the name and email to be filled out.

Form Defaults

The comment_form_defaults is very much like the comment_form_default_fields in that it returns an associative array as well. The possible keys are:

  • fields
  • comment_field
  • must_log_in
  • logged_in_as
  • comment_notes_before
  • comment_notes_after
  • action
  • id_form
  • id_submit
  • class_form
  • class_submit
  • name_submit
  • title_reply
  • title_reply_to
  • title_reply_before
  • title_reply_after
  • cancel_reply_before
  • cancel_reply_after
  • cancel_reply_link
  • label_submit
  • submit_button
  • submit_field
  • format

Very much like the previous filter we can use something like:

add_filter( 'comment_form_defaults', 'jmc_change_comment_form' );
function jmc_change_comment_form( $defaults ) {

	// Change the "cancel" to "I would rather not comment" and use a span instead
	$defaults['cancel_reply_before'] = '<span class="cancel-reply">';
	$defaults['cancel_reply_link'] = __( 'I would rather not comment', 'text-domain' );
	$defaults['cancle_reply_acter'] = '</span>';

	return $defaults;
}

It is very important that you return that array otherwise your fields will not show.

Published by

Jose

Born in El Salvador. Loves to read, write, draw, paint, build, test, typography, hike, photography, art, design, sewing, and many other things.