WordPress函数:comment_form 输出一个完整的评论表单

江河/ 2023年06月02日/ WordPress/ 浏览 906

函数原型:


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


输出一个完整的评论表单,以便在模板中使用。


大多数字符串和表单字段可以通过传递到函数中的$args数组进行控制,而如果您只想添加一个新字段或删除一个字段,也可以选择使用“comment_form_default_fields”过滤器来修改默认字段的数组。所有字段还分别通过“comment_form_field_$name”的过滤器,其中$name是字段数组中使用的键。


参数说明:


$args,要重写的默认参数和表单字段。


-- fields,默认评论字段,默认情况下可通过“comment_form_Default_fields”挂钩进行筛选。

---- author,评论作者字段。

---- email,评论作者 Email 字段。

---- url,评论作者 URL 字段。

---- cookies,评论 cookie 选择加入字段。


-- comment_field,评论文本区域字段HTML。


-- must_log_in,“必须登录才能发表评论”消息的HTML元素。


-- logged_in_as,“以[用户]身份登录”消息的HTML、“编辑配置文件”链接和“注销”链接。


-- comment_notes_before,如果用户未登录,则在评论字段之前显示消息的HTML元素。默认为“您的电子邮件地址将不会发布。”。


-- comment_notes_after,文本区域字段后显示的消息的HTML元素。


-- action,评论表单元素操作属性。默认为“/wp comments post.php”。


-- id_form,评论表单元素id属性。默认“评论表单”。


-- id_submit,comment submit元素id属性。默认“提交”。


-- class_container,评论表单容器类属性。默认为“评论响应”。


-- class_form,评论表单元素类属性。默认的“评论表单”。


-- class_submit,comment submit元素类属性。默认“提交”。


-- name_submit,comment submit元素名称属性。默认为“提交”。


-- title_reply,可翻译的“回复”按钮标签。默认为“留下回复”。


-- title_reply_to,可翻译的“回复”按钮标签。默认为“给%s留下回复”,其中%s是要回复的评论的作者。


-- title_reply_before,评论表单标题前显示的HTML。默认:<h3 id="reply-title" class="comment-reply-title">


-- title_reply_after,评论表单标题后显示的HTML。默认:</h3>


-- cancel_reply_before,取消回复链接前显示的HTML。


-- cancel_reply_after,取消回复链接后显示的HTML。


-- cancel_reply_link,可翻译的“取消回复”按钮标签。默认为“取消回复”。


-- label_submit,可翻译的“提交”按钮标签。可翻译的”提交“按钮标签。默认为“发表评论”。


-- submit_button,“提交”按钮的HTML格式。默认:<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />.


-- submit_field,围绕“提交”按钮和评论隐藏字段的标记的HTML格式。默认值:<p class=“form submit”>%1$s%2$s</p>,其中%1$s是提交按钮标记,%2$s是评论隐藏字段。


-- format,可用值:xhtml, html5


$post,要为其生成表单的Post ID或WP_Post对象。默认当前帖子。


函数源码:


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 ) ) {
		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'];
		}
	}

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

	$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'] ) && false === strpos( $args['comment_notes_before'], 'id="email-notes"' ) ) {
		$args['fields']['email'] = str_replace(
			' aria-describedby="email-notes"',
			'',
			$args['fields']['email']
		);
	}

	do_action( 'comment_form_before' );
	?>
	<div id="respond" class="<?php echo esc_attr( $args['class_container'] ); ?>">
		<?php
		echo $args['title_reply_before'];

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

		if ( get_option( 'thread_comments' ) ) {
			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'];

			do_action( 'comment_form_must_log_in_after' );

		else :

			printf(
				'<form action="%s" method="post" id="%s" class="%s"%s>',
				esc_url( $args['action'] ),
				esc_attr( $args['id_form'] ),
				esc_attr( $args['class_form'] ),
				( $html5 ? ' novalidate' : '' )
			);

			do_action( 'comment_form_top' );

			if ( is_user_logged_in() ) :

				echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );

				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'];

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

					echo apply_filters( 'comment_form_field_comment', $field );

					echo $args['comment_notes_after'];

				} elseif ( ! is_user_logged_in() ) {

					if ( $first_field === $name ) {
						do_action( 'comment_form_before_fields' );
					}

					echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";

					if ( $last_field === $name ) {
						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'] )
			);

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

			echo apply_filters( 'comment_form_submit_field', $submit_field, $args );

			do_action( 'comment_form', $post_id );

			echo '</form>';

		endif;
		?>
	</div><!-- #respond -->
	<?php

	do_action( 'comment_form_after' );
}


包含钩子:


do_action( 'comment_form', int $post_id )
// Fires at the bottom of the comment form, inside the closing form tag.

do_action( 'comment_form_after' )
// Fires after the comment form.

do_action( 'comment_form_after_fields' )
// Fires after the comment fields in the comment form, excluding the textarea.

do_action( 'comment_form_before' )
// Fires before the comment form.

do_action( 'comment_form_before_fields' )
// Fires before the comment fields in the comment form, excluding the textarea.

do_action( 'comment_form_comments_closed' )
// Fires after the comment form if comments are closed.

apply_filters( 'comment_form_defaults', array $defaults )
// Filters the comment form default arguments.

apply_filters( 'comment_form_default_fields', string[] $fields )
// Filters the default comment form fields.

apply_filters( 'comment_form_fields', array $comment_fields )
// Filters the comment form fields, including the textarea.

apply_filters( 'comment_form_field_comment', string $args_comment_field )
// Filters the content of the comment textarea field for display.

apply_filters( "comment_form_field_{$name}", string $field )
// Filters a comment form field for display.

apply_filters( 'comment_form_logged_in', string $args_logged_in, array $commenter, string $user_identity )
// Filters the ‘logged in’ message for the comment form for display.

do_action( 'comment_form_logged_in_after', array $commenter, string $user_identity )
// Fires after the is_user_logged_in() check in the comment form.

do_action( 'comment_form_must_log_in_after' )
// Fires after the HTML-formatted ‘must log in after’ message in the comment form.

apply_filters( 'comment_form_submit_button', string $submit_button, array $args )
// Filters the submit button for the comment form to display.

apply_filters( 'comment_form_submit_field', string $submit_field, array $args )
// Filters the submit field for the comment form to display.

do_action( 'comment_form_top' )
// Fires at the top of the comment form, inside the form tag.

apply_filters( 'the_permalink', string $permalink, int|WP_Post $post )
// Filters the display of the permalink for the current post.



使用举例:


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


发表评论

暂无评论,抢个沙发...

客服 工单