comments.php
の中で使う、入力フォーム部分に当たる、comment_form()
のカスタム方法を説明。
今回は、functions.php
を使わない方法のみになります。
仕様
<?php comment_form( $args, $post_id ); ?>
パラメータ | 初期値 | 説明 |
$args | 下記項目参照 | |
$post_id | (空) | フォームを生成する投稿IDを指定。 空の場合、現在の投稿。 |
$args
2019年末時点での、Codex日本語版に掲載されている内容になります。
現在の最新版である、5.3.2では一部異なる部分があります。
<?php
$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>'
)
),
);
?>
カスタム例
タイトルとボタンの文字を変更する
<?php
$args = array(
// タイトルの「コメントを残す」の文字列を変更
'title_reply' => 'コメントする',
// reply用のJSがオフの場合、「usernameにコメントする」となる。
// reply用のJSがオンの場合、title_replyが使われる。
'title_reply_to' => '%s に返信する',
// 返信を押した後の「コメントをキャンセル」の文字列を変更
'cancel_reply_link' => '取り消す',
// 送信ボタンの「コメントを送信」の文字列を変更
'label_submit' => '送信する',
);
comment_form( $args );
?>
タイトル下の文字やクラス名を変更
改行の設定や、リンク領域の変更などを行い、見やすく出来ます。
また、それぞれのクラス名の変更も可能。
<?php
$args = array(
//ユーザー登録してログインしたユーザーのみコメントをつけられるようにする設定がONの場合。
'must_log_in' => '<p class="must-log-in">' . sprintf( __( 'コメントを残すには<a href="%s">ログイン</a>してください。' ), wp_login_url( apply_filters( 'the_permalink', get_permalink() ) ) ) . '</p>',
//ログイン中の場合
'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( '<a href="%1$s">%2$s</a>としてログイン中です。<br><a href="%3$s" title="Log out of this account">ログアウトする。</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>',
//通常のゲスト
'comment_notes_before' => '<p class="comment-notes">' . __( 'メールアドレスが公開されることはありません。<br>' ) . ( get_option( 'require_name_email' ) ? sprintf( ' ' . __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) : '' ) . '</p>',
);
comment_form( $args );
?>
入力フォームの文字やクラス名を変更
フォームに、placeholderや独自のクラス名を付与できます。
<?php
$args = array(
'comment_field' => '<p class="comment-form-comment"><label for="comment">' . 'メッセージ' . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true">' . '</textarea></p>',
'fields' => apply_filters( 'comment_form_default_fields', array(
'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'ハンドルネーム' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' . '<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">' . __( 'メルアド' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' . '<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30" placeholder="mail@example.com" ' . $aria_req . ' /></p>',
//不要な場合は、空にする
'url' => ''
) ),
);
comment_form( $args );
?>
emailなども非表示にする場合、ディスカッション設定の「 コメントの投稿者の名前とメールアドレスの入力を必須にする」を解除します。
CSSでも消すことができます。
.comment-form-email,
.comment-form-url{ display:none; }
まとめ
いろいろとカスタムできますが、できるだけ管理画面のディスカッション設定で制御できる状態を残しておいた方が、後々の管理がラクになりますよ〜