KH-WEBLOG TOP > WEBメモ > WordPress > コメントフォームのカスタマイズ:WordPress

コメントフォームのカスタマイズ:WordPress

WordPressでコメントフォームをカスタマイズする方法をメモ。

WordPressでコメントフォームを表示させるときは
comment_form();
を使いますが、こちらで出力されるソースを編集する際は、
comment_form.phpの中の表示させたい部分に
まず、

<?php 
//コメントの指定
$defaults = array(
	'fields'               => apply_filters( 'comment_form_default_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" 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( $post_id ) ) ) ) . '</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( $post_id ) ) ) ) . '</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>',
	'id_form'              => 'commentform'//出力されるid名の指定,
	'id_submit'            => 'submit'//出力されるid名の指定,
	'title_reply'          => 'コメントを残す',//title_reply部分の文言
	'title_reply_to'       => __( 'Leave a Reply to %s' ),
	'cancel_reply_link'    => __( 'Cancel reply' ),
	'label_submit'         => __( '送 信' ),//送信ボタンの文言
); ?>

のようにある程度、文言などを修正します。
この中のfieldsというのは、名前・アドレス・URLの部分を出力しているのですが、この部分を編集する際は、

<?php
//$fieldsの指定
$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部分のソース、文言
	'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ' ' . ( $req ? '<span class="required">*</span></label>' : '' ) .
	            '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
//url部分のソース、文言
	'url'    => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label>' .
	            '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
); ?>

のようにfieldsの値を編集すればOK
例えば、URLの部分がいらないなら

'url'    => '',

のように空にすれば表示されなくなります。

最後に、上記で指定したコメントフォームを出力する際は、

<?php comment_form($defaults); ?>

とすればOK

細かい部分に関してはよくわからないので
WordPress Codex 日本語版
を参考にしていただければと思います。

コーディング・WordPress化作業を代行します

WordPressのおすすめ参考書

楽天Kobo電子書籍ストア
¥3,278 (2024/04/19 11:27時点 | 楽天市場調べ)

TAGS

.htaccess ActionScript All in one seo pack Contact Form 7 CSS CSS3 EC-CUBE Flash HTML HTML5 JavaScript jQuery LightBox PHP RSS SEO WordPress アイキャッチ画像 アクセス解析 カスタムフィールド カテゴリー カラーミーショップ カート コメント ショートコード テンプレートタグ ドロップダウンメニュー パーマリンク フォーム フルスクリーン ブログカード プラグイン ページテンプレート ページナビ ページ分割 マウスイベント リダイレクト リンク リンクカード レンタルサーバー ロールオーバー 携帯サイト 条件分岐 正規表現 関連記事