我已经在用户年龄的wordpress评论表中添加了一个额外的字段.我已经像这样添加了字段:
add_filter('comment_form_default_fields','add_comment_fields');
function add_comment_fields($fields) {
$fields['location'] = '<p class="comment-form-location"><label for="location">' . __( Location ) . '</label>' .
'<input id="location" name="location" type="text" size="30" /></p>';
return $fields;
}
Run Code Online (Sandbox Code Playgroud)
而且我还通过使用'comment_post'动作将meta值保存在注释元表中.现在我必须在管理员评论部分显示这个评论元值.我怎样才能做到这一点 ?
小智 10
尝试下面的代码,希望你能在管理部分看到你的额外字段(这里我使用age作为元键).
add_action( 'add_meta_boxes_comment', 'comment_add_meta_box' );
function comment_add_meta_box()
{
add_meta_box( 'my-comment-title', __( 'Your field title' ), 'comment_meta_box_age', 'comment', 'normal', 'high' );
}
function comment_meta_box_age( $comment )
{
$title = get_comment_meta( $comment->comment_ID, 'age', true );
?>
<p>
<label for="age"><?php _e( 'Your label Name' ); ?></label>;
<input type="text" name="age" value="<?php echo esc_attr( $title ); ?>" class="widefat" />
</p>
<?php
}
add_action( 'edit_comment', 'comment_edit_function' );
function comment_edit_function( $comment_id )
{
if( isset( $_POST['age'] ) )
update_comment_meta( $comment_id, 'age', esc_attr( $_POST['age'] ) );
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2435 次 |
| 最近记录: |