返回介绍

comment_form()

发布于 2017-09-10 21:50:42 字数 21257 浏览 1307 评论 0 收藏 0

comment_form( array $args = array(),  int|WP_Post $post_id = null )

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.


参数

$args

(array) (Optional) 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.
  • '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) HTML element for a 'logged in as [user]' message.
  • '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'.
  • 'id_form'
    (string) The comment form element id attribute. Default 'commentform'.
  • 'id_submit'
    (string) The comment submit element id attribute. Default 'submit'.
  • '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: '</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 value: array()

$post_id

(int|WP_Post) (Optional) Post ID or WP_Post object to generate the form for. Default current post.

Default value: null


源代码

File: wp-includes/comment-template.php

function comment_form( $args = array(), $post_id = null ) {
	if ( null === $post_id )
		$post_id = get_the_ID();

	// Exit the function when comments for the post are closed.
	if ( ! comments_open( $post_id ) ) {
		/**
		 * Fires after the comment form if comments are closed.
		 *
		 * @since 3.0.0
		 */
		do_action( 'comment_form_comments_closed' );

		return;
	}

	$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' );
	$aria_req = ( $req ? " aria-required='true'" : '' );
	$html_req = ( $req ? " required='required'" : '' );
	$html5    = 'html5' === $args['format'];
	$fields   =  array(
		'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $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 . ' /></p>',
		'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
		            '<input id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" maxlength="100" aria-describedby="email-notes"' . $aria_req . $html_req  . ' /></p>',
		'url'    => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
		            '<input id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" maxlength="200" /></p>',
	);

	$required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required">*</span>' );

	/**
	 * Filters the default comment form fields.
	 *
	 * @since 3.0.0
	 *
	 * @param array $fields The default comment fields.
	 */
	$fields = apply_filters( 'comment_form_default_fields', $fields );
	$defaults = array(
		'fields'               => $fields,
		'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" aria-required="true" required="required"></textarea></p>',
		/** This filter is documented in wp-includes/link-template.php */
		'must_log_in'          => '<p class="must-log-in">' . sprintf(
		                              /* translators: %s: login URL */
		                              __( 'You must be <a href="%s">logged in</a> to post a comment.' ),
		                              wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) )
		                          ) . '</p>',
		/** This filter is documented in wp-includes/link-template.php */
		'logged_in_as'         => '<p class="logged-in-as">' . sprintf(
		                              /* translators: 1: edit user link, 2: accessibility text, 3: user name, 4: logout URL */
		                              __( '<a href="%1$s" aria-label="%2$s">Logged in as %3$s</a>. <a href="%4$s">Log out?</a>' ),
		                              get_edit_user_link(),
		                              /* translators: %s: user name */
		                              esc_attr( sprintf( __( 'Logged in as %s. Edit your profile.' ), $user_identity ) ),
		                              $user_identity,
		                              wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) )
		                          ) . '</p>',
		'comment_notes_before' => '<p class="comment-notes"><span id="email-notes">' . __( 'Your email address will not be published.' ) . '</span>'. ( $req ? $required_text : '' ) . '</p>',
		'comment_notes_after'  => '',
		'action'               => site_url( '/wp-comments-post.php' ),
		'id_form'              => 'commentform',
		'id_submit'            => 'submit',
		'class_form'           => 'comment-form',
		'class_submit'         => 'submit',
		'name_submit'          => 'submit',
		'title_reply'          => __( 'Leave a Reply' ),
		'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 {@see '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 args contain all required default values.
	$args = array_merge( $defaults, $args );

	/**
	 * Fires before the comment form.
	 *
	 * @since 3.0.0
	 */
	do_action( 'comment_form_before' );
	?>
	<div id="respond" class="comment-respond">
		<?php
		echo $args['title_reply_before'];

		comment_form_title( $args['title_reply'], $args['title_reply_to'] );

		echo $args['cancel_reply_before'];

		cancel_comment_reply_link( $args['cancel_reply_link'] );

		echo $args['cancel_reply_after'];

		echo $args['title_reply_after'];

		if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) :
			echo $args['must_log_in'];
			/**
			 * Fires after the HTML-formatted 'must log in after' message in the comment form.
			 *
			 * @since 3.0.0
			 */
			do_action( 'comment_form_must_log_in_after' );
		else : ?>
			<form action="<?php echo esc_url( $args['action'] ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>" class="<?php echo esc_attr( $args['class_form'] ); ?>"<?php echo $html5 ? ' novalidate' : ''; ?>>
				<?php
				/**
				 * 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 logged-in-as HTML-formatted message.
					 * @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() ) {

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 filter hook, `$name`, refers to the name
 * of the comment form field. Such as 'author', 'email', or 'url'.
 *
 * @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 );
				?>
			</form>
		<?php endif; ?>
	</div><!--

更新日志

Versiondescription
4.6.0Introduced the 'action' argument.
4.5.0The 'author', 'email', and 'url' form fields are limited to 245, 100, and 200 characters, respectively.
4.4.0Introduced the 'class_form', 'title_reply_before', 'title_reply_after', 'cancel_reply_before', and 'cancel_reply_after' arguments.
4.2.0Introduced the 'submit_button' and 'submit_fields' arguments.
4.1.0Introduced the 'class_submit' argument.
3.0.0Introduced.

相关函数

Uses

  • wp-includes/comment-template.php: comment_form_fields
  • wp-includes/comment-template.php: comment_form_submit_button
  • wp-includes/comment-template.php: comment_form_submit_field
  • wp-includes/theme.php: current_theme_supports()
  • wp-includes/l10n.php: __()
  • wp-includes/l10n.php: _x()
  • wp-includes/formatting.php: esc_attr()
  • wp-includes/formatting.php: esc_url()
  • wp-includes/pluggable.php: is_user_logged_in()
  • wp-includes/pluggable.php: wp_get_current_user()
  • wp-includes/general-template.php: wp_login_url()
  • wp-includes/general-template.php: wp_logout_url()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/link-template.php: site_url()
  • wp-includes/link-template.php: get_edit_user_link()
  • wp-includes/link-template.php: get_permalink()
  • wp-includes/link-template.php: the_permalink
  • wp-includes/plugin.php: do_action()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_option()
  • wp-includes/post-template.php: get_the_ID()
  • wp-includes/comment-template.php: comment_form_comments_closed
  • wp-includes/comment-template.php: comment_form_logged_in
  • wp-includes/comment-template.php: comment_form_logged_in_after
  • wp-includes/comment-template.php: comment_form_field_comment
  • wp-includes/comment-template.php: comment_form_before_fields
  • wp-includes/comment-template.php: comment_form_field_{$name}
  • wp-includes/comment-template.php: comment_form_after_fields
  • wp-includes/comment-template.php: comment_form
  • wp-includes/comment-template.php: comment_form_after
  • wp-includes/comment-template.php: comment_form_title()
  • wp-includes/comment-template.php: cancel_comment_reply_link()
  • wp-includes/comment-template.php: get_comment_id_fields()
  • wp-includes/comment-template.php: comment_form_default_fields
  • wp-includes/comment-template.php: comment_form_defaults
  • wp-includes/comment-template.php: comment_form_before
  • wp-includes/comment-template.php: comment_form_must_log_in_after
  • wp-includes/comment-template.php: comment_form_top
  • wp-includes/comment-template.php: comments_open()
  • wp-includes/comment.php: wp_get_current_commenter()
  • Show 35 more uses Hide more uses

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    Simple example how to change some comment form fields:

    
    $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 );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文