给你:Adding Custom Fields to WordPress Comment Forms?
还有另一个很棒的帖子:http://wpengineer.com/2214/adding-input-fields-to-the-comment-form/
功能可用于添加/更新、删除评论元,类似于帖子和用户元。
Edit:下面是一个让您开始的示例(将代码放入functions.php
或在自定义插件中):
将字段添加到注释表单:
add_filter( \'comment_form_defaults\', \'change_comment_form_defaults\');
function change_comment_form_defaults( $default ) {
$commenter = wp_get_current_commenter();
$default[ \'fields\' ][ \'email\' ] .= \'<p class="comment-form-author">\' .
\'<label for="city">\'. __(\'City\') . \'</label>
<span class="required">*</span>
<input id="city" name="city" size="30" type="text" /></p>\';
return $default;
}
4个用于检索/添加/更新/删除注释元的函数:
get_comment_meta( $comment_id, $meta_key, $single = false );
add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false );
update_comment_meta($comment_id, $meta_key, $meta_value, $unique = false );
delete_comment_meta( $comment_id, $meta_key, $single = false );
这是进行验证的地方:
add_filter( \'preprocess_comment\', \'verify_comment_meta_data\' );
function verify_comment_meta_data( $commentdata ) {
if ( ! isset( $_POST[\'city\'] ) )
wp_die( __( \'Error: please fill the required field (city).\' ) );
return $commentdata;
}
并保存评论元:
add_action( \'comment_post\', \'save_comment_meta_data\' );
function save_comment_meta_data( $comment_id ) {
add_comment_meta( $comment_id, \'city\', $_POST[ \'city\' ] );
}
检索并显示注释元:
add_filter( \'get_comment_author_link\', \'attach_city_to_author\' );
function attach_city_to_author( $author ) {
$city = get_comment_meta( get_comment_ID(), \'city\', true );
if ( $city )
$author .= " ($city)";
return $author;
}
(
Note: 所有代码都来自
WPengineer 我在上面发布的链接。那篇文章有更多的细节和高级用法,请也检查一下!)