wordpress给评论添加自定义字段,添加我们想要保存的其他留言评论信息,当然也可以修改和删除。和文章、用户、分类等添加自定义字段有所不同,它更加简单,
新增例子:Movies这个类型的文章评论添加年龄这个字段,示例代码如下:
function add_comment_fields($fields) {
if( is_singular( 'Movies' ) ) {//这里限制文章类型,比如页面page,文章post,又比如自定义的文章类型Movies
$fields['age'] = '<p class="comment-form-age"><label for="age">' . __( 'Age' ) . '</label>' .
'<input id="age" name="age" type="text" size="30" /></p>';
}
return $fields;
}add_filter('comment_form_default_fields','add_comment_fields');
还可以进行排序,将某个字段移到最后:
function prefix_move_comment_field_to_bottom( $fields ) {
$comment_field = $fields['comment'];
unset( $fields['comment'] );
$fields['comment'] = $comment_field;
return $fields;
}
add_filter( 'comment_form_fields','prefix_move_comment_field_to_bottom', 10, 1 );
修改字段,可以修改/删除我们添加的新字段,也可以修改/删除默认的字段:
add_filter( 'comment_form_fields', 'custom_comment_field' );
function custom_comment_field( $fields ) {
// 想更改的字段
$comment_field = $fields['author'];
$comment_field = $fields['email'];
$comment_field = $fields['comment'];
$comment_field = $fields['cookies'];
// 移除字段
unset($fields['author']);
unset($fields['email']);
unset($fields['url']);
unset($fields['comment']);
unset($fields['cookies']);
// 自定义你的字段
// The order in which you place them will determine in what order they are displayed.
$fields['author'] = '<p class="comment-form-author"><label for="author">Name <span class="required">*</span></label><input type="text" id="author" name="author" require="required" placeholder="Name"></p>';
$fields['email'] = '<p class="comment-form-email"><label for="email">Email <span class="required">*</span></label><input type="text" id="email" name="email" require="required" placeholder="Email"></p>';
$fields['comment'] = '<p class="comment-form-comment"><label for="comment">Comment <span class="required">*</span></label><textarea id="comment" name="comment" required="required" placeholder="Comment"></textarea></p>';
$fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"><label for="wp-comment-cookies-consent">Save details for future comments?</label></p>';
return $fields;
}
上面的就包含所有评论字段的操作,可以进行新增、修改和删除,极其简单。
© 版权声明
THE END
暂无评论内容