WordPress のコメント管理画面でカスタムフィールドを編集できるようにする

WordPress の管理画面のコメント編集画面でカスタムフィールドを編集できるようにする。

編集画面をカスタマイズする

wp-admin/edit-form-comment.php に追記する。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$address = echo get_comment_meta( $comment->comment_ID, 'address', true );
?>
<tr>
<td class="first"><label for="newcomment_author_address">住所</label></td>
<td>
<input
type="text"
id="newcomment_author_address"
name="address"
size="30"
class="code"
value="<?php $address; ?>" />
</td>
</tr>

保存機能をカスタマイズする

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 口コミのメタデータの保存処理
*
* @param int $comment_id
*/
function save_custom_comment_field($comment_id) {
if ( ! $comment = get_comment( $comment_id ) ) {
return false;
}
$custom_key_address = 'address';
$address = esc_attr($_POST[$custom_key_address]);

if('' == get_comment_meta($comment_id, $custom_key_address)) {
add_comment_meta($comment_id, $custom_key_address, $address, true);
} else if($address != get_comment_meta( $comment_id, $custom_key_address)) {
update_comment_meta($comment_id, $custom_key_address, $address);
} else if('' == $address) {
delete_comment_meta($comment_id, $custom_key_address);
}

return false;
}
add_action('comment_post', 'save_custom_comment_field');
add_action('edit_comment', 'save_custom_comment_field');