コメントの項目足してみるパン

WordPress のコメントの部分に項目を足すことは、サイトを検索すると引っかかりますが、
保存するところまでは説明されていませんでした。。。

そこで、データベースを見ていたら、commentmeta というテーブルがあるではないですか!
っということで、commentmetaを利用し、追加した項目を保存してみましょう。

とりあえず、テーマファイルの function.php に以下のソースを追加します。

// Comment Customize
add_action( 'comment_form_field_comment', 'add_comment_field' );
function add_comment_field( $defaults ) {
	$defaults = '<p class="comment-form-twitter"><label for="twitter">twitter@</label><input id="twitter" name="twitter" type="text" value="" size="15" /></p>';
	$defaults .= '<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>';
	return $defaults;
}

add_action( 'comment_post', 'save_comment_field' );
add_action( 'edit_comment', 'save_comment_field' );
function save_comment_field( $comment_id ) {
	if ( !$comment = get_comment( $comment_id ) )
		return false;
	do_action('comment_form', $post->ID);
	$custom_key_twitter  = 'twitter';
    $get_comment_twitter = esc_attr( $_POST[$custom_key_twitter] );
    if ( "" == get_comment_meta( $comment_id, $custom_key_twitter )) {
        add_comment_meta( $comment_id, $custom_key_twitter, $get_comment_twitter, true ) ;
    } else if ( $get_comment_twitter != get_comment_meta( $comment_id, $custom_key_twitter )) {
        update_comment_meta( $comment_id, $custom_key_twitter, $get_comment_twitter ) ;
    } else if ( "" == $get_comment_twitter ) {
        delete_comment_meta( $comment_id, $custom_key_twitter ) ;
    }
	return false;
}

add_action( 'add_meta_boxes_comment', 'add_comment_field_box' );
function add_comment_field_box() {
    global $comment;
	$comment_ID = $comment->comment_ID;
    $custom_key         = 'post_reviews_date' ;
    $custom_key_twitter = 'twitter' ;
    $noncename          = $custom_key . '_noncename' ;
    $get_twitter        = esc_attr( get_comment_meta( $comment_ID, $custom_key_twitter, true ) );
    echo '<input type="hidden" name="' . $noncename . '" id="' . $noncename . '" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />' . "\n";
	echo '<p class="comment-form-twitter"><label for="twitter">twitter@</label><input id="' . $custom_key_twitter . '" name="' . $custom_key_twitter . '" type="text" value="' . $get_twitter . '" size="15" /></p>' . "\n";
}

add_comment_field でコメントの項目を足しています。

$defaults .= '<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>';

上記は必ず入れてください。
入れないとコメントのテキストエリアが表示されません。

function add_comment_field( $fields ) {
	$fields['twitter'] = '<p class="comment-form-twitter"><label for="twitter">twitter@</label><input id="twitter" name="twitter" type="text" value="" size="15" /></p>';
	return $fields;
}
add_filter('comment_form_default_fields','add_comment_field');

もありますが、ログインした際は表示されないので目的に合わせて変更してください。

add_action( 'comment_post', 'save_comment_field' );
add_action( 'edit_comment', 'save_comment_field' );
function save_comment_field( $comment_id ) {
	if ( !$comment = get_comment( $comment_id ) )
		return false;
	do_action('comment_form', $post->ID);
	$custom_key_twitter  = 'twitter';
    $get_comment_twitter = esc_attr( $_POST[$custom_key_twitter] );
    if ( "" == get_comment_meta( $comment_id, $custom_key_twitter )) {
        add_comment_meta( $comment_id, $custom_key_twitter, $get_comment_twitter, true ) ;
    } else if ( $get_comment_twitter != get_comment_meta( $comment_id, $custom_key_twitter )) {
        update_comment_meta( $comment_id, $custom_key_twitter, $get_comment_twitter ) ;
    } else if ( "" == $get_comment_twitter ) {
        delete_comment_meta( $comment_id, $custom_key_twitter ) ;
    }
	return false;
}

で commentmeta に保存します。

add_action( 'add_meta_boxes_comment', 'add_comment_field_box' );
function add_comment_field_box() {
    global $comment;
	$comment_ID = $comment->comment_ID;
    $custom_key         = 'post_reviews_date' ;
    $custom_key_twitter = 'twitter' ;
    $noncename          = $custom_key . '_noncename' ;
    $get_twitter        = esc_attr( get_comment_meta( $comment_ID, $custom_key_twitter, true ) );
    echo '<input type="hidden" name="' . $noncename . '" id="' . $noncename . '" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />' . "\n";
	echo '<p class="comment-form-twitter"><label for="twitter">twitter@</label><input id="' . $custom_key_twitter . '" name="' . $custom_key_twitter . '" type="text" value="' . $get_twitter . '" size="15" /></p>' . "\n";
}

で管理画面からのコメント編集の際にコメントに追加したボックスが表示されます。

コメントに表示するときは comment.php 内に

wp_list_comments();

があると思うのでそれを

wp_list_comments( array( 'callback' => 'mytheme_comment' ) );

などにして、mytheme_comment の関数を読み込みます。

mytheme_comment の関数は

function mytheme_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment;
   $comment_ID = $comment->comment_ID;
   $get_twitter        = esc_attr( get_comment_meta( $comment->comment_ID, 'twitter', true ) ); ?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
     <div id="comment-<?php comment_ID(); ?>">
      <div class="comment-author vcard">
         <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>

         <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
         <?php printf(__('Twitter ID %s'), $get_twitter) ?>
      </div>
      <?php if ($comment->comment_approved == '0') : ?>
         <em><?php _e('Your comment is awaiting moderation.') ?></em>
         <br />
      <?php endif; ?>

      <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','') ?></div>

      <?php comment_text() ?>

      <div class="reply">
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
      </div>
     </div>
<?php
}

等にして function.php に記入します。
今回保存した項目の読み出しは

get_comment_meta( $comment->comment_ID, 'twitter', true )

で読みだしています。
get_comment_meta は get_post_meta と同じような感じです。

以上、文章がおかしいですが大体の流れが分かって頂ければと思います。

This entry was posted in 未分類. Bookmark the permalink.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>