【WordPress】コメントのカスタマイズ

WordPressのコメントまわりのカスタマイズをいろいろ触ったメモのまとめです。

    コメント投稿フォームの出力

    comment_form();

    コメントの有無の判定

    have_comments();またはget_comments_number();

    コメント一覧の表示

    wp_list_comments( $args, $comments );

    出力の変更

    $argsに指定したコールバック関数で行う。

    これを使えば、各コメントを表示するとき WordPress 内部の機能をバイパスし、代わりにカスタム関数を呼び出します。HTML レイアウトを極端に変えてコメントを表示するために使ってください。 テンプレートタグ/wp list comments – WordPress Codex 日本語版
    PHP
    function callback_func( $comment, $args, $depth ) {
    	$GLOBALS['comment'] = $comment;
    	<li>
    		// 任意の出力(閉じタグは不要)
    	//</li>
    }
    • $comment : WP_Comment オブジェクト
    • $args : wp_list_comments に渡した $args
    • $depth : コメントの深さ

    コメントしたユーザーの情報を取り出す

    WP_Comment オブジェクトにアクセスする。

    例)メールアドレスなら$comment->comment_author_email

    cookieを利用する

    cookieを利用するには、Cookie オプトイン用チェックボックスを表示してチェックを入れてもらう必要がある。

    設定→ディスカッション設定より、「コメント投稿者が Cookie を保存できるようにする、Cookie オプトイン用チェックボックスを表示します」にチェックを入れて保存する。

    comment_form() 内で使う場合

    $fields内でデフォルトの入力項目を設定するのに使える。$commenter内に格納されている。

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

    コールバック関数内などで使う場合

    wp_get_current_commenter()で取得する。

    以下が利用できる。

    • comment_author
    • comment_author_email
    • comment_author_url

    「次回のコメントで使用するためブラウザーに自分の名前、メールアドレス、サイトを保存する。」のテキストを変更する

    日本語表記だけ変更する

    翻訳ファイルにアクセスして変更します。

    PHP
    add_filter( 'gettext', 'change_comment_cookies_text', 20, 3 );
    function change_comment_cookies_text( $translated_text, $text, $domain ){
    	switch( $text ){
    		case 'Save my name, email, and website in this browser for the next time I comment.' :
    		$translated_text = '変更後のテキスト';
    		break;
    	}
    	return $translated_text;
    }

    まるっと表示を変更する

    フィルターフックcomment_form_default_fieldsにアクセスしてcookieキーに格納された情報を変更します。デフォルトは以下です。

    国際化の対応が不要なら、翻訳だけ触るのが安全な気がします。

    PHP
    if ( has_action( 'set_comment_cookies', 'wp_set_comment_cookies' ) && get_option( 'show_comments_cookies_opt_in' ) ) {
    	$consent = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';
    
    	$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'];
    	}
    }

    サポートが必要ですか?

    ご質問・お見積り依頼はお気軽にどうぞ

    お問い合わせはこちら
    シェア
    野良人 代表
    新免祥太
    1988年岡山生まれ。外食企業のWEB・EC担当を経験したのち、2013年12月より「野良人(のらんど)」の屋号で独立しWEBデザイン・プログラミングなどWEBサイト制作の工程全般を請け負っています。お気軽にご相談ください。
    広告
    次の記事(2020/12/31)
    フォームでメールアドレスを扱う
    前の記事(2020/12/31)
    【Stripe】ダッシュボードで作成した商品のCheckout決済を実装する
    記事一覧