万が一、当サイトで重大な問題を発見した際などは、フォーラムや WordSlack #docs チャンネルでお知らせください。</p>
関数リファレンス/comment form
目次
概要
このタグはテンプレート内で完全なコメントフォームを出力します。
ほとんどの文字列とフォームフィールドは関数に渡される $args 配列を通してコントロールできますが、フィールドを一つだけ付け足したり削除したい場合には comment_form_default_fields
フィルターを使ってデフォルトフィールドの配列を編集することもできます。また、すべてのフィールドはフォームの comment_form_field_$name
フィルターを通して個別に渡されます。ここで、$name
はフィールド配列で使われたキーになります。
使い方
<?php comment_form( $args, $post_id ); ?>
デフォルトの設定
<?php comment_form(); ?>
Twenty Ten では wp-content/themes/twentyten/comments.php
で呼び出されています。
パラメータ
- post_id
- (mixed) (オプション) Nullの場合、現在の投稿を使用するフォームを生成する投稿ID。
- 初期値: null (the current post)
$args
注: $new_defaults を使っているコメントテンプレート内で $defaults を変更する場合、$new_defaults は comment_form($new_defaults);
を呼び出す前に定義する必要があります。そうでないと正しく反映されません。
デフォルト値:
- fields
- (array) (オプション) '作成者', 'メールアドレス', 'URL'の入力フィールド。
- 初期値:
apply_filters( 'comment_form_default_fields', $fields )
- 初期値:
- comment_field
- (文字列) (オプション) The textarea and the label of comment body.
- 初期値:
'<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>'
- 初期値:
- must_log_in
- (文字列) (オプション)
- 初期値:
'<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'
- 初期値:
- logged_in_as
- (文字列) (オプション)
- 初期値:
'<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'
- 初期値:
- comment_notes_before
- (文字列) (オプション) Text or HTML to be displayed before the set of comment form fields if the user is not logged in.
- 初期値:
'<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>'
- 初期値:
- comment_notes_after
- (文字列) (オプション) Text or HTML to be displayed after the set of comment fields (and before the submit button)
- 初期値:
'<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>'
- 初期値:
- id_form
- (文字列) (オプション) value of the
id
attribute ofform
element (<form>
tag).- 初期値: 'commentform'
- id_submit
- (文字列) (オプション) value of the
id
attribute of submit button.- 初期値: 'submit'
- title_reply
- (文字列) (オプション) The title of comment form (when not replying to a comment, see comment_form_title).
- 初期値: __( 'Leave a Reply' )
- title_reply_to
- (文字列) (オプション) The title of comment form (when replying to a comment, see comment_form_title).
- 初期値: __( 'Leave a Reply to %s' )
- cancel_reply_link
- (文字列) (オプション) link label to cancel reply.
- 初期値: __( 'Cancel reply' )
- label_submit
- (文字列) (オプション) 送信ボタンの名前
- 初期値: __( 'Post Comment' )
$fields
デフォルト入力項目:
$fields = array( 'author' => '<p class="comment-form-author"><label for="author">' . __( 'Name', 'domainreference' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>', 'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email', 'domainreference' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>', 'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website', 'domainreference' ) . '</label>' . '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>', );
Note: To use the variables present in the above code in a custom callback function, you must first set these variables within your callback using:
$commenter = wp_get_current_commenter(); $req = get_option( 'require_name_email' ); $aria_req = ( $req ? " aria-required='true'" : '' );
デフォルト $args 配列
$args = array(
'id_form' => 'commentform',
'id_submit' => 'submit',
'title_reply' => __( 'Leave a Reply' ),
'title_reply_to' => __( 'Leave a Reply to %s' ),
'cancel_reply_link' => __( 'Cancel Reply' ),
'label_submit' => __( 'Post Comment' ),
'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) .
'</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true">' .
'</textarea></p>',
'must_log_in' => '<p class="must-log-in">' .
sprintf(
__( 'You must be <a href="%s">logged in</a> to post a comment.' ),
wp_login_url( apply_filters( 'the_permalink', get_permalink() ) )
) . '</p>',
'logged_in_as' => '<p class="logged-in-as">' .
sprintf(
__( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ),
admin_url( 'profile.php' ),
$user_identity,
wp_logout_url( apply_filters( 'the_permalink', get_permalink( ) ) )
) . '</p>',
'comment_notes_before' => '<p class="comment-notes">' .
__( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) .
'</p>',
'comment_notes_after' => '<p class="form-allowed-tags">' .
sprintf(
__( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ),
' <code>' . allowed_tags() . '</code>'
) . '</p>',
'fields' => apply_filters( 'comment_form_default_fields', array(
'author' =>
'<p class="comment-form-author">' .
'<label for="author">' . __( 'Name', 'domainreference' ) . '</label> ' .
( $req ? '<span class="required">*</span>' : '' ) .
'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
'" size="30"' . $aria_req . ' /></p>',
'email' =>
'<p class="comment-form-email"><label for="email">' . __( 'Email', 'domainreference' ) . '</label> ' .
( $req ? '<span class="required">*</span>' : '' ) .
'<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) .
'" size="30"' . $aria_req . ' /></p>',
'url' =>
'<p class="comment-form-url"><label for="url">' .
__( 'Website', 'domainreference' ) . '</label>' .
'<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
'" size="30" /></p>'
)
),
);
戻り値
void
例
コメントフォームフィールドの一部を変更する簡単な例。
$comments_args = array( // 送信ボタンのタイトルを変更 'label_submit'=>'Send', // 返信セクションのタイトルを変更 'title_reply'=>'Write a Reply or Comment', // "Text or HTML to be displayed after the set of comment fields" を削除 'comment_notes_after' => '', // オリジナルの textarea (コメント本文入力欄) を再定義 '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);
フィルターフックの使用
- comment_form_default_fields [en]
- the_permalink /en
- comment_form_defaults [en]
- comment_form_logged_in [en]
- comment_form_field_{$name} [en]
- comment_form_field_comment [en]
プラガブルアクション
- comment_form_before [en]
- comment_form_must_log_in_after [en]
- comment_form_top [en]
- comment_form_logged_in_after [en]
- comment_form_before_fields [en]
- comment_form_after_fields [en]
- comment_form [en]
- comment_form_after [en]
- comment_form_comments_closed [en]
変更履歴
- 3.0.0: 新規テンプレートタグ
ソースコード
comment_form()
は wp-includes/comment-template.php
にあります。
関連
コメント関数: cancel_comment_reply_link(), comment_author(), comment_author_email(), comment_author_email_link(), comment_author_IP(), comment_author_link(), comment_author_rss(), comment_author_url(), comment_author_url_link(), comment_class(), comment_date(), comment_excerpt(), comment_form_title(), comment_form(), comment_ID(), comment_id_fields() / en, comment_reply_link(), comment_text(), comment_text_rss(), comment_time(), comment_type(), comments_link, comments_number(), comments_open(), comments_popup_link(), comments_popup_script(), comments_rss_link(), get_avatar(), next_comments_link(), paginate_comments_links(), permalink_comments_rss(), previous_comments_link(), wp_list_comments()
最新英語版: WordPress Codex » Template Tags/comment_form (最新版との差分)