函数文档

comment_form()

💡 云策文档标注

概述

comment_form() 函数用于在模板中输出完整的评论表单。它通过 $args 参数数组控制表单字段和字符串,并支持多个过滤器钩子进行自定义。

关键要点

  • 函数输出一个完整的评论表单,适用于主题模板。
  • 通过 $args 数组参数可覆盖默认设置,如字段 HTML、标签、类名等。
  • 支持过滤器钩子如 'comment_form_default_fields' 和 'comment_form_field_$name' 来修改字段。
  • 自动处理登录状态、评论关闭情况,并集成 HTML5 或 XHTML 格式。
  • 包含多个动作钩子(如 comment_form_before)和过滤器钩子(如 comment_form_submit_button)用于扩展。

代码示例

$comments_args = array(
    'label_submit' => __( 'Send', 'textdomain' ),
    'title_reply' => __( 'Write a Reply or Comment', 'textdomain' ),
    'comment_notes_after' => '',
    'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea></p>',
);
comment_form( $comments_args );

注意事项

  • 如果评论已关闭或文章无效,函数会触发 comment_form_comments_closed 动作并返回。
  • 默认字段包括 author、email、url、cookies 和 comment_field,可通过过滤器调整。
  • 参数如 format 支持 'html5' 或 'xhtml',影响表单的 HTML 语法。
  • 使用过滤器时,注意钩子名称和参数类型,如 comment_form_fields 用于所有字段包括文本域。

📄 原文内容

Outputs a complete commenting form for use within a template.

Description

Most strings and form fields may be controlled through the $args array passed into the function, while you may also choose to use the ‘comment_form_default_fields’ filter to modify the array of default fields if you’d just like to add a new one or remove a single field. All fields are also individually passed through a filter of the ‘comment_form_field_$name’ where $name is the key used in the array of fields.

Parameters

$argsarrayoptional
Default arguments and form fields to override.

  • fields array
    Default comment fields, filterable by default via the ‘comment_form_default_fields’ hook.

    • author string
      Comment author field HTML.
    • email string
      Comment author email field HTML.
    • url string
      Comment author URL field HTML.
    • cookies string
      Comment cookie opt-in field HTML.
  • comment_field string
    The comment textarea field HTML.
  • must_log_in string
    HTML element for a ‘must be logged in to comment’ message.
  • logged_in_as string
    The HTML for the ‘logged in as [user]’ message, the Edit profile link, and the Log out link.
  • comment_notes_before string
    HTML element for a message displayed before the comment fields if the user is not logged in.
    Default ‘Your email address will not be published.’.
  • comment_notes_after string
    HTML element for a message displayed after the textarea field.
  • action string
    The comment form element action attribute. Default '/wp-comments-post.php'.
  • novalidate bool
    Whether the novalidate attribute is added to the comment form. Default false.
  • id_form string
    The comment form element id attribute. Default 'commentform'.
  • id_submit string
    The comment submit element id attribute. Default 'submit'.
  • class_container string
    The comment form container class attribute. Default 'comment-respond'.
  • class_form string
    The comment form element class attribute. Default 'comment-form'.
  • class_submit string
    The comment submit element class attribute. Default 'submit'.
  • name_submit string
    The comment submit element name attribute. Default 'submit'.
  • title_reply string
    The translatable 'reply' button label. Default ‘Leave a Reply’.
  • title_reply_to string
    The translatable 'reply-to' button label. Default ‘Leave a Reply to %s’, where %s is the author of the comment being replied to.
  • title_reply_before string
    HTML displayed before the comment form title.
    Default: <h3 id="reply-title" class="comment-reply-title">.
  • title_reply_after string
    HTML displayed after the comment form title.
    Default: <code></h3>.
  • cancel_reply_before string
    HTML displayed before the cancel reply link.
  • cancel_reply_after string
    HTML displayed after the cancel reply link.
  • cancel_reply_link string
    The translatable ‘cancel reply’ button label. Default ‘Cancel reply’.
  • label_submit string
    The translatable 'submit' button label. Default ‘Post a comment’.
  • submit_button string
    HTML format for the Submit button.
    Default: <input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />.
  • submit_field string
    HTML format for the markup surrounding the Submit button and comment hidden fields. Default: <p class="form-submit">%1$s %2$s</p>, where %1$s is the submit button markup and %2$s is the comment hidden fields.
  • format string
    The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'.

Default:array()

$postint|WP_Postoptional
Post ID or WP_Post object to generate the form for. Default current post.

Default:null

Source

function comment_form( $args = array(), $post = null ) {
	$post = get_post( $post );

	// Exit the function if the post is invalid or comments are closed.
	if ( ! $post || ! comments_open( $post ) ) {
		/**
		 * Fires after the comment form if comments are closed.
		 *
		 * For backward compatibility, this action also fires if comment_form()
		 * is called with an invalid post object or ID.
		 *
		 * @since 3.0.0
		 */
		do_action( 'comment_form_comments_closed' );

		return;
	}

	$post_id       = $post->ID;
	$commenter     = wp_get_current_commenter();
	$user          = wp_get_current_user();
	$user_identity = $user->exists() ? $user->display_name : '';

	$args = wp_parse_args( $args );
	if ( ! isset( $args['format'] ) ) {
		$args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
	}

	$req   = get_option( 'require_name_email' );
	$html5 = 'html5' === $args['format'];

	// Define attributes in HTML5 or XHTML syntax.
	$required_attribute = ( $html5 ? ' required' : ' required="required"' );
	$checked_attribute  = ( $html5 ? ' checked' : ' checked="checked"' );

	// Identify required fields visually and create a message about the indicator.
	$required_indicator = ' ' . wp_required_field_indicator();
	$required_text      = ' ' . wp_required_field_message();

	$fields = array(
		'author' => sprintf(
			'<p class="comment-form-author">%s %s</p>',
			sprintf(
				'<label for="author">%s%s</label>',
				__( 'Name' ),
				( $req ? $required_indicator : '' )
			),
			sprintf(
				'<input id="author" name="author" type="text" value="%s" size="30" maxlength="245" autocomplete="name"%s />',
				esc_attr( $commenter['comment_author'] ),
				( $req ? $required_attribute : '' )
			)
		),
		'email'  => sprintf(
			'<p class="comment-form-email">%s %s</p>',
			sprintf(
				'<label for="email">%s%s</label>',
				__( 'Email' ),
				( $req ? $required_indicator : '' )
			),
			sprintf(
				'<input id="email" name="email" %s value="%s" size="30" maxlength="100" aria-describedby="email-notes" autocomplete="email"%s />',
				( $html5 ? 'type="email"' : 'type="text"' ),
				esc_attr( $commenter['comment_author_email'] ),
				( $req ? $required_attribute : '' )
			)
		),
		'url'    => sprintf(
			'<p class="comment-form-url">%s %s</p>',
			sprintf(
				'<label for="url">%s</label>',
				__( 'Website' )
			),
			sprintf(
				'<input id="url" name="url" %s value="%s" size="30" maxlength="200" autocomplete="url" />',
				( $html5 ? 'type="url"' : 'type="text"' ),
				esc_attr( $commenter['comment_author_url'] )
			)
		),
	);

	if ( has_action( 'set_comment_cookies', 'wp_set_comment_cookies' ) && get_option( 'show_comments_cookies_opt_in' ) ) {
		$consent = empty( $commenter['comment_author_email'] ) ? '' : $checked_attribute;

		$fields['cookies'] = sprintf(
			'<p class="comment-form-cookies-consent">%s %s</p>',
			sprintf(
				'<input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"%s />',
				$consent
			),
			sprintf(
				'<label for="wp-comment-cookies-consent">%s</label>',
				__( 'Save my name, email, and website in this browser for the next time I comment.' )
			)
		);

		// Ensure that the passed fields include cookies consent.
		if ( isset( $args['fields'] ) && ! isset( $args['fields']['cookies'] ) ) {
			$args['fields']['cookies'] = $fields['cookies'];
		}
	}

	$original_fields = $fields;

	/**
	 * Filters the default comment form fields.
	 *
	 * @since 3.0.0
	 *
	 * @param string[] $fields Array of the default comment fields.
	 */
	$fields = apply_filters( 'comment_form_default_fields', $fields );

	$defaults = array(
		'fields'               => $fields,
		'comment_field'        => sprintf(
			'<p class="comment-form-comment">%s %s</p>',
			sprintf(
				'<label for="comment">%s%s</label>',
				_x( 'Comment', 'noun' ),
				$required_indicator
			),
			'<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525"' . $required_attribute . '></textarea>'
		),
		'must_log_in'          => sprintf(
			'<p class="must-log-in">%s</p>',
			sprintf(
				/* translators: %s: Login URL. */
				__( 'You must be <a href="%s">logged in</a> to post a comment.' ),
				/** This filter is documented in wp-includes/link-template.php */
				wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) )
			)
		),
		'logged_in_as'         => sprintf(
			'<p class="logged-in-as">%s%s</p>',
			sprintf(
				/* translators: 1: User name, 2: Edit user link, 3: Logout URL. */
				__( 'Logged in as %1$s. <a href="%2$s">Edit your profile</a>. <a href="%3$s">Log out?</a>' ),
				$user_identity,
				get_edit_user_link(),
				/** This filter is documented in wp-includes/link-template.php */
				wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) )
			),
			$required_text
		),
		'comment_notes_before' => sprintf(
			'<p class="comment-notes">%s%s</p>',
			sprintf(
				'<span id="email-notes">%s</span>',
				__( 'Your email address will not be published.' )
			),
			$required_text
		),
		'comment_notes_after'  => '',
		'action'               => site_url( '/wp-comments-post.php' ),
		'novalidate'           => false,
		'id_form'              => 'commentform',
		'id_submit'            => 'submit',
		'class_container'      => 'comment-respond',
		'class_form'           => 'comment-form',
		'class_submit'         => 'submit',
		'name_submit'          => 'submit',
		'title_reply'          => __( 'Leave a Reply' ),
		/* translators: %s: Author of the comment being replied to. */
		'title_reply_to'       => __( 'Leave a Reply to %s' ),
		'title_reply_before'   => '<h3 id="reply-title" class="comment-reply-title">',
		'title_reply_after'    => '</h3>',
		'cancel_reply_before'  => ' <small>',
		'cancel_reply_after'   => '</small>',
		'cancel_reply_link'    => __( 'Cancel reply' ),
		'label_submit'         => __( 'Post Comment' ),
		'submit_button'        => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',
		'submit_field'         => '<p class="form-submit">%1$s %2$s</p>',
		'format'               => 'xhtml',
	);

	/**
	 * Filters the comment form default arguments.
	 *
	 * Use 'comment_form_default_fields' to filter the comment fields.
	 *
	 * @since 3.0.0
	 *
	 * @param array $defaults The default comment form arguments.
	 */
	$args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );

	// Ensure that the filtered arguments contain all required default values.
	$args = array_merge( $defaults, $args );

	// Remove `aria-describedby` from the email field if there's no associated description.
	if ( isset( $args['fields']['email'] ) && ! str_contains( $args['comment_notes_before'], 'id="email-notes"' ) ) {
		$args['fields']['email'] = str_replace(
			' aria-describedby="email-notes"',
			'',
			$args['fields']['email']
		);
	}

	/**
	 * Fires before the comment form.
	 *
	 * @since 3.0.0
	 */
	do_action( 'comment_form_before' );
	?>
	<div id="respond" class="<?php echo esc_attr( $args['class_container'] ); ?>">
		',
				esc_url( $args['action'] ),
				esc_attr( $args['id_form'] ),
				esc_attr( $args['class_form'] ),
				( $args['novalidate'] ? ' novalidate' : '' )
			);

			/**
			 * Fires at the top of the comment form, inside the form tag.
			 *
			 * @since 3.0.0
			 */
			do_action( 'comment_form_top' );

			if ( is_user_logged_in() ) :

				/**
				 * Filters the 'logged in' message for the comment form for display.
				 *
				 * @since 3.0.0
				 *
				 * @param string $args_logged_in The HTML for the 'logged in as [user]' message,
				 *                               the Edit profile link, and the Log out link.
				 * @param array  $commenter      An array containing the comment author's
				 *                               username, email, and URL.
				 * @param string $user_identity  If the commenter is a registered user,
				 *                               the display name, blank otherwise.
				 */
				echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );

				/**
				 * Fires after the is_user_logged_in() check in the comment form.
				 *
				 * @since 3.0.0
				 *
				 * @param array  $commenter     An array containing the comment author's
				 *                              username, email, and URL.
				 * @param string $user_identity If the commenter is a registered user,
				 *                              the display name, blank otherwise.
				 */
				do_action( 'comment_form_logged_in_after', $commenter, $user_identity );

			else :

				echo $args['comment_notes_before'];

			endif;

			// Prepare an array of all fields, including the textarea.
			$comment_fields = array( 'comment' => $args['comment_field'] ) + (array) $args['fields'];

			/**
			 * Filters the comment form fields, including the textarea.
			 *
			 * @since 4.4.0
			 *
			 * @param array $comment_fields The comment fields.
			 */
			$comment_fields = apply_filters( 'comment_form_fields', $comment_fields );

			// Get an array of field names, excluding the textarea.
			$comment_field_keys = array_diff( array_keys( $comment_fields ), array( 'comment' ) );

			// Get the first and the last field name, excluding the textarea.
			$first_field = reset( $comment_field_keys );
			$last_field  = end( $comment_field_keys );

			foreach ( $comment_fields as $name => $field ) {

				if ( 'comment' === $name ) {

					/**
					 * Filters the content of the comment textarea field for display.
					 *
					 * @since 3.0.0
					 *
					 * @param string $args_comment_field The content of the comment textarea field.
					 */
					echo apply_filters( 'comment_form_field_comment', $field );

					echo $args['comment_notes_after'];

				} elseif ( ! is_user_logged_in() || ! isset( $original_fields[ $name ] ) ) {

					if ( $first_field === $name ) {
						/**
						 * Fires before the comment fields in the comment form, excluding the textarea.
						 *
						 * @since 3.0.0
						 */
						do_action( 'comment_form_before_fields' );
					}

					/**
					 * Filters a comment form field for display.
					 *
					 * The dynamic portion of the hook name, `$name`, refers to the name
					 * of the comment form field.
					 *
					 * Possible hook names include:
					 *
					 *  - `comment_form_field_comment`
					 *  - `comment_form_field_author`
					 *  - `comment_form_field_email`
					 *  - `comment_form_field_url`
					 *  - `comment_form_field_cookies`
					 *
					 * @since 3.0.0
					 *
					 * @param string $field The HTML-formatted output of the comment form field.
					 */
					echo apply_filters( "comment_form_field_{$name}", $field ) . "n";

					if ( $last_field === $name ) {
						/**
						 * Fires after the comment fields in the comment form, excluding the textarea.
						 *
						 * @since 3.0.0
						 */
						do_action( 'comment_form_after_fields' );
					}
				}
			}

			$submit_button = sprintf(
				$args['submit_button'],
				esc_attr( $args['name_submit'] ),
				esc_attr( $args['id_submit'] ),
				esc_attr( $args['class_submit'] ),
				esc_attr( $args['label_submit'] )
			);

			/**
			 * Filters the submit button for the comment form to display.
			 *
			 * @since 4.2.0
			 *
			 * @param string $submit_button HTML markup for the submit button.
			 * @param array  $args          Arguments passed to comment_form().
			 */
			$submit_button = apply_filters( 'comment_form_submit_button', $submit_button, $args );

			$submit_field = sprintf(
				$args['submit_field'],
				$submit_button,
				get_comment_id_fields( $post_id )
			);

			/**
			 * Filters the submit field for the comment form to display.
			 *
			 * The submit field includes the submit button, hidden fields for the
			 * comment form, and any wrapper markup.
			 *
			 * @since 4.2.0
			 *
			 * @param string $submit_field HTML markup for the submit field.
			 * @param array  $args         Arguments passed to comment_form().
			 */
			echo apply_filters( 'comment_form_submit_field', $submit_field, $args );

			/**
			 * Fires at the bottom of the comment form, inside the closing form tag.
			 *
			 * @since 1.5.0
			 *
			 * @param int $post_id The post ID.
			 */
			do_action( 'comment_form', $post_id );

			echo '</form>';

		endif;
		?>
	</div><!-- #respond -->
	</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/comment-template.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/comment-template.php#L2494">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/comment-template.php#L2494-L2912">View on GitHub</a></p></section>
		<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form/"><span class="hook-func">do_action</span>( ‘comment_form’,  <nobr><span class="arg-type">int</span> <span class="arg-name">$post_id</span></nobr> )</a></dt><dd><p>Fires at the bottom of the comment form, inside the closing form tag.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_after/"><span class="hook-func">do_action</span>( ‘comment_form_after’ )</a></dt><dd><p>Fires after the comment form.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_after_fields/"><span class="hook-func">do_action</span>( ‘comment_form_after_fields’ )</a></dt><dd><p>Fires after the comment fields in the comment form, excluding the textarea.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_before/"><span class="hook-func">do_action</span>( ‘comment_form_before’ )</a></dt><dd><p>Fires before the comment form.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_before_fields/"><span class="hook-func">do_action</span>( ‘comment_form_before_fields’ )</a></dt><dd><p>Fires before the comment fields in the comment form, excluding the textarea.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_comments_closed/"><span class="hook-func">do_action</span>( ‘comment_form_comments_closed’ )</a></dt><dd><p>Fires after the comment form if comments are closed.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_defaults/"><span class="hook-func">apply_filters</span>( ‘comment_form_defaults’,  <nobr><span class="arg-type">array</span> <span class="arg-name">$defaults</span></nobr> )</a></dt><dd><p>Filters the comment form default arguments.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_default_fields/"><span class="hook-func">apply_filters</span>( ‘comment_form_default_fields’,  <nobr><span class="arg-type">string[]</span> <span class="arg-name">$fields</span></nobr> )</a></dt><dd><p>Filters the default comment form fields.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_fields/"><span class="hook-func">apply_filters</span>( ‘comment_form_fields’,  <nobr><span class="arg-type">array</span> <span class="arg-name">$comment_fields</span></nobr> )</a></dt><dd><p>Filters the comment form fields, including the textarea.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_field_comment/"><span class="hook-func">apply_filters</span>( ‘comment_form_field_comment’,  <nobr><span class="arg-type">string</span> <span class="arg-name">$args_comment_field</span></nobr> )</a></dt><dd><p>Filters the content of the comment textarea field for display.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_field_name/"><span class="hook-func">apply_filters</span>( “comment_form_field_{$name}”,  <nobr><span class="arg-type">string</span> <span class="arg-name">$field</span></nobr> )</a></dt><dd><p>Filters a comment form field for display.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_logged_in/"><span class="hook-func">apply_filters</span>( ‘comment_form_logged_in’,  <nobr><span class="arg-type">string</span> <span class="arg-name">$args_logged_in</span></nobr>,  <nobr><span class="arg-type">array</span> <span class="arg-name">$commenter</span></nobr>,  <nobr><span class="arg-type">string</span> <span class="arg-name">$user_identity</span></nobr> )</a></dt><dd><p>Filters the ‘logged in’ message for the comment form for display.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_logged_in_after/"><span class="hook-func">do_action</span>( ‘comment_form_logged_in_after’,  <nobr><span class="arg-type">array</span> <span class="arg-name">$commenter</span></nobr>,  <nobr><span class="arg-type">string</span> <span class="arg-name">$user_identity</span></nobr> )</a></dt><dd><p>Fires after the <a href="https://developer.wordpress.org/reference/functions/is_user_logged_in/" rel="function">is_user_logged_in()</a>  check in the comment form.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_must_log_in_after/"><span class="hook-func">do_action</span>( ‘comment_form_must_log_in_after’ )</a></dt><dd><p>Fires after the HTML-formatted ‘must log in after’ message in the comment form.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_submit_button/"><span class="hook-func">apply_filters</span>( ‘comment_form_submit_button’,  <nobr><span class="arg-type">string</span> <span class="arg-name">$submit_button</span></nobr>,  <nobr><span class="arg-type">array</span> <span class="arg-name">$args</span></nobr> )</a></dt><dd><p>Filters the submit button for the comment form to display.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_submit_field/"><span class="hook-func">apply_filters</span>( ‘comment_form_submit_field’,  <nobr><span class="arg-type">string</span> <span class="arg-name">$submit_field</span></nobr>,  <nobr><span class="arg-type">array</span> <span class="arg-name">$args</span></nobr> )</a></dt><dd><p>Filters the submit field for the comment form to display.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/comment_form_top/"><span class="hook-func">do_action</span>( ‘comment_form_top’ )</a></dt><dd><p>Fires at the top of the comment form, inside the form tag.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/the_permalink/"><span class="hook-func">apply_filters</span>( ‘the_permalink’,  <nobr><span class="arg-type">string</span> <span class="arg-name">$permalink</span></nobr>,  <nobr><span class="arg-type">int|WP_Post</span> <span class="arg-name">$post</span></nobr> )</a></dt><dd><p>Filters the display of the permalink for the current post.</p>
</dd></dl></section>
		<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_required_field_message/">wp_required_field_message()</a><code>wp-includes/general-template.php</code></td><td><p>Creates a message to explain required form fields.</p>
</td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_required_field_indicator/">wp_required_field_indicator()</a><code>wp-includes/general-template.php</code></td><td><p>Assigns a visual indicator for required form fields.</p>
</td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_get_current_user/">wp_get_current_user()</a><code>wp-includes/pluggable.php</code></td><td><p>Retrieves the current user object.</p>
</td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_login_url/">wp_login_url()</a><code>wp-includes/general-template.php</code></td><td><p>Retrieves the login URL.</p>
</td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_logout_url/">wp_logout_url()</a><code>wp-includes/general-template.php</code></td><td><p>Retrieves the logout URL.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/site_url/">site_url()</a><code>wp-includes/link-template.php</code></td><td><p>Retrieves the URL for the current site where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/get_edit_user_link/">get_edit_user_link()</a><code>wp-includes/link-template.php</code></td><td><p>Retrieves the edit user link.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/has_action/">has_action()</a><code>wp-includes/plugin.php</code></td><td><p>Checks if any action has been registered for a hook.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/comment_form_title/">comment_form_title()</a><code>wp-includes/comment-template.php</code></td><td><p>Displays text based on comment reply status.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/cancel_comment_reply_link/">cancel_comment_reply_link()</a><code>wp-includes/comment-template.php</code></td><td><p>Displays HTML content for cancel comment reply link.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/get_comment_id_fields/">get_comment_id_fields()</a><code>wp-includes/comment-template.php</code></td><td><p>Retrieves hidden input HTML for replying to comments.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/comments_open/">comments_open()</a><code>wp-includes/comment-template.php</code></td><td><p>Determines whether the current post is open for comments.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/wp_get_current_commenter/">wp_get_current_commenter()</a><code>wp-includes/comment.php</code></td><td><p>Gets current commenter’s name, email, and URL.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/current_theme_supports/">current_theme_supports()</a><code>wp-includes/theme.php</code></td><td><p>Checks a theme’s support for a given feature.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/__/">__()</a><code>wp-includes/l10n.php</code></td><td><p>Retrieves the translation of $text.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/_x/">_x()</a><code>wp-includes/l10n.php</code></td><td><p>Retrieves translated string with gettext context.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/esc_attr/">esc_attr()</a><code>wp-includes/formatting.php</code></td><td><p>Escaping for HTML attributes.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/esc_url/">esc_url()</a><code>wp-includes/formatting.php</code></td><td><p>Checks and cleans a URL.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/is_user_logged_in/">is_user_logged_in()</a><code>wp-includes/pluggable.php</code></td><td><p>Determines whether the current visitor is a logged in user.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/wp_parse_args/">wp_parse_args()</a><code>wp-includes/functions.php</code></td><td><p>Merges user defined arguments into defaults array.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/get_permalink/">get_permalink()</a><code>wp-includes/link-template.php</code></td><td><p>Retrieves the full permalink for the current post or post ID.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/do_action/">do_action()</a><code>wp-includes/plugin.php</code></td><td><p>Calls the callback functions that have been added to an action hook.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/apply_filters/">apply_filters()</a><code>wp-includes/plugin.php</code></td><td><p>Calls the callback functions that have been added to a filter hook.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/get_option/">get_option()</a><code>wp-includes/option.php</code></td><td><p>Retrieves an option value based on an option name.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/get_post/">get_post()</a><code>wp-includes/post.php</code></td><td><p>Retrieves post data given a post ID or post object.</p>
</td></tr></tbody></table></figure><a class="wp-block-wporg-code-table-show-more" href="#">Show 20 more</a><a class="wp-block-wporg-code-table-show-less" href="#">Show less</a></section>  </section>
		<section class="wp-block-wporg-code-reference-changelog"><h2 id="changelog" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#changelog">Changelog</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table"><figure class="wp-block-table "><table><thead><tr><th scope="col">Version</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/since/6.8.2/">6.8.2</a></td><td><span class="since-description">Introduced the <code>'novalidate'</code> argument.</span></td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/since/5.5.0/">5.5.0</a></td><td><span class="since-description">Introduced the <code>'class_container'</code> argument.</span></td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/since/4.9.6/">4.9.6</a></td><td><span class="since-description">Introduced the <code>'cookies'</code> default comment field.</span></td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/since/4.6.0/">4.6.0</a></td><td><span class="since-description">Introduced the <code>'action'</code> argument.</span></td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/since/4.5.0/">4.5.0</a></td><td><span class="since-description">The <code>'author'</code>, <code>'email'</code>, and <code>'url'</code> form fields are limited to 245, 100,              and 200 characters, respectively.</span></td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/since/4.4.0/">4.4.0</a></td><td><span class="since-description">Introduced the <code>'class_form'</code>, <code>'title_reply_before'</code>, <code>'title_reply_after'</code>,              <code>'cancel_reply_before'</code>, and <code>'cancel_reply_after'</code> arguments.</span></td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/since/4.2.0/">4.2.0</a></td><td><span class="since-description">Introduced the <code>'submit_button'</code> and <code>'submit_fields'</code> arguments.</span></td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/since/4.1.0/">4.1.0</a></td><td><span class="since-description">Introduced the <code>'class_submit'</code> argument.</span></td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/since/3.0.0/">3.0.0</a></td><td>Introduced.</td></tr></tbody></table></figure><a class="wp-block-wporg-code-table-show-more" href="#">Show 4 more</a><a class="wp-block-wporg-code-table-show-less" href="#">Show less</a></section> </section>
		<section class="wp-block-wporg-code-reference-comments" data-nosnippet="true"><h2 id="user-contributed-notes" class="is-toc-heading wp-block-heading" tabindex="-1" ><a href="#user-contributed-notes">User Contributed Notes</a></h2> <ol class="comment-list">			<li id="comment-439" data-comment-id="439" class="comment byuser comment-author-codex odd alt thread-odd thread-alt depth-1">
			<article id="div-comment-439" class="comment-body">

							<a href="#comment-content-439" class="screen-reader-text">Skip to note 4 content</a>
				<header class="comment-meta">
					<div class="comment-author vcard">
						<span class="comment-author-attribution">
						<a href="https://profiles.wordpress.org/codex/" rel="external nofollow" class="url">Codex</a>						</span>
						<a class="comment-date" href="https://developer.wordpress.org/reference/functions/comment_form/#comment-439">
							<time datetime="2015-07-02T07:44:28+00:00">
							11 years ago							</time>
						</a>

																													</div>
					<div class="user-note-voting" data-nonce="18c3ad059d" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="439" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fcomment_form%2F%23comment-439"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title="83% like this"><span class="screen-reader-text">Vote results for this note: </span>12</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="439" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fcomment_form%2F%23comment-439"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div>				</header>
				<!-- .comment-metadata -->
			
				<div class="wporg-has-embedded-code comment-content" id="comment-content-439">
				<p><strong>Simple example how to change some comment form fields:</strong></p>
<pre class="wp-block-code"><code lang="php" class="language-php line-numbers">$comments_args = array(
        // Change the title of send button 
        'label_submit' => __( 'Send', 'textdomain' ),
        // Change the title of the reply section
        'title_reply' => __( 'Write a Reply or Comment', 'textdomain' ),
        // 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"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><br /><textarea id="comment" name="comment" aria-required="true"></textarea></p>',
);
comment_form( $comments_args );

  • Skip to note 5 content

    Order of the Comment Fields > Functions.php

    //Comment Field Order
    add_filter( 'comment_form_fields', 'mo_comment_fields_custom_order' );
    function mo_comment_fields_custom_order( $fields ) {
        $comment_field = $fields['comment'];
        $author_field = $fields['author'];
        $email_field = $fields['email'];
        $url_field = $fields['url'];
        $cookies_field = $fields['cookies'];
        unset( $fields['comment'] );
        unset( $fields['author'] );
        unset( $fields['email'] );
        unset( $fields['url'] );
        unset( $fields['cookies'] );
        // the order of fields is the order below, change it as needed:
        $fields['author'] = $author_field;
        $fields['email'] = $email_field;
        $fields['url'] = $url_field;
        $fields['comment'] = $comment_field;
        $fields['cookies'] = $cookies_field;
        // done ordering, now return the fields:
        return $fields;
    }

  • Skip to note 6 content

    Expanded list of Translate Friendly options -> comments.php

    //Declare Vars
    $comment_send = 'Send';
    $comment_reply = 'Leave a Message';
    $comment_reply_to = 'Reply';
    
    $comment_author = 'Name';
    $comment_email = 'E-Mail';
    $comment_body = 'Comment';
    $comment_url = 'Website';
    $comment_cookies_1 = ' By commenting you accept the';
    $comment_cookies_2 = ' Privacy Policy';
    
    $comment_before = 'Registration isn't required.';
    
    $comment_cancel = 'Cancel Reply';
    
    //Array
    $comments_args = array(
        //Define Fields
        'fields' => array(
            //Author field
            'author' => '<p class="comment-form-author"><br /><input id="author" name="author" aria-required="true" placeholder="' . $comment_author .'"></input></p>',
            //Email Field
            'email' => '<p class="comment-form-email"><br /><input id="email" name="email" placeholder="' . $comment_email .'"></input></p>',
            //URL Field
            'url' => '<p class="comment-form-url"><br /><input id="url" name="url" placeholder="' . $comment_url .'"></input></p>',
            //Cookies
            'cookies' => '<input type="checkbox" required>' . $comment_cookies_1 . '<a href="' . get_privacy_policy_url() . '">' . $comment_cookies_2 . '</a>',
        ),
        // Change the title of send button
        'label_submit' => __( $comment_send ),
        // Change the title of the reply section
        'title_reply' => __( $comment_reply ),
        // Change the title of the reply section
        'title_reply_to' => __( $comment_reply_to ),
        //Cancel Reply Text
        'cancel_reply_link' => __( $comment_cancel ),
        // Redefine your own textarea (the comment body).
        'comment_field' => '<p class="comment-form-comment"><br /><textarea id="comment" name="comment" aria-required="true" placeholder="' . $comment_body .'"></textarea></p>',
        //Message Before Comment
        'comment_notes_before' => __( $comment_before),
        // Remove "Text or HTML to be displayed after the set of comment fields".
        'comment_notes_after' => '',
        //Submit Button ID
        'id_submit' => __( 'comment-submit' ),
    );
    comment_form( $comments_args );

  • You must log in before being able to contribute a note or feedback.