[英]Can't edit comment meta data in Wordpress
I am using Wordpress (version 4.5.3) and have added a custom field to the comment form. 我正在使用Wordpress(4.5.3版),并已在注释表单中添加了一个自定义字段。 The field is a drop down list using the select tag.
该字段是使用选择标记的下拉列表。 The field shows up in the form just fine, and I can choose an option and the value for that option gets saved properly.
该字段以很好的形式显示,我可以选择一个选项,并且该选项的值会正确保存。
I have also added code to edit the custom field in the Edit Comment page (/wordpress/wp-admin/comment.php?action=editcomment). 我还添加了代码来编辑“编辑评论”页面中的自定义字段(/wordpress/wp-admin/comment.php?action=editcomment)。 I am able to retrieve the stored value and then recreate the drop down list with the proper value selected.
我能够检索存储的值,然后使用选定的正确值重新创建下拉列表。
I have added code to save the edited value for the custom field but that is not working. 我添加了代码以保存自定义字段的已编辑值,但无法正常工作。 When, from the Edit Comment page, I select a different value from the drop down and then click on the "update" button the newly selected value does not get saved.
当我从“编辑评论”页面上从下拉列表中选择另一个值,然后单击“更新”按钮时,新选择的值将不会保存。
I am adding the code to do all this in functions.php. 我将添加代码以在functions.php中完成所有这些操作。 Here's the code to add the field to the form and store the data:
这是将字段添加到表单并存储数据的代码:
// Add fields after default fields above the comment box, always visible
add_action( 'comment_form_logged_in_after', 'additional_fields' );
add_action( 'comment_form_after_fields', 'additional_fields' );
function additional_fields () {
echo '<p class="comment-form-area">'.
'<label for="region">' . __( 'Choose a <strong>region</strong>' ) . '<span class="required">* </span></label>'.
'<br /><select id="region" name="region">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select></p>';
}
// Save the comment meta data along with comment
add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {
if ( ( isset( $_POST['region'] ) ) && ( $_POST['region'] != '') )
$region = wp_filter_nohtml_kses($_POST['region']);
add_comment_meta( $comment_id, 'region', $region );
}
Here is the code to add the custom field to the comment editing page: 以下是将自定义字段添加到评论编辑页面的代码:
// Add an edit option to comment editing screen
add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );
function extend_comment_add_meta_box() {
add_meta_box( 'title', __( 'Region field' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' );
}
function extend_comment_meta_box ( $comment ) {
$_regions = array
("one",
"two",
"three",
"four");
$region = get_comment_meta( $comment->comment_ID, 'region', true );
?>
<p>
<label for="region"><?php _e( 'Region*' ); ?></label>
<p><select id="region" name="region">
<?php
for ($ix = 0; $ix < count($_regions); $ix++) {
echo '<option value="' . $_regions[$ix] . '"';
if ($region == $_regions[$ix]) {
echo ' selected';
}
echo '>' . $_regions[$ix] . '</option>';
}
?>
</select></p>
<?php
}
All this seems to be working fine. 所有这些似乎都很好。 Here is the code to save the edited value for the custom field:
以下是用于保存自定义字段的已编辑值的代码:
// Update comment meta data from comment editing screen
add_action( 'edit_comment', 'extend_comment_edit_metafields' );
function extend_comment_edit_metafields( $comment_id ) {
if( ! isset( $_POST['extend_comment_update'] ) || ! wp_verify_nonce( $_POST['extend_comment_update'], 'extend_comment_update' ) ) return;
if ( ( isset( $_POST['region'] ) ) && ( $_POST['region'] != '') ) :
$region = wp_filter_nohtml_kses($_POST['region']);
update_comment_meta( $comment_id, 'region', $region );
else :
delete_comment_meta( $comment_id, 'region');
endif;
}
This code does not work. 此代码不起作用。 When I return to the Edit Comment page I see the original value for the custom field, not the edited value.
当我返回“编辑注释”页面时,看到的是自定义字段的原始值,而不是编辑后的值。
Why isn't the edited value being saved? 为什么不保存已编辑的值?
The function extend_comment_edit_metafields() initially tests to see if the nonce field has been set. 函数extend_comment_edit_metafields()最初进行测试以查看是否已设置了现时字段。 It has not so the return executes without calling update_comment_meta().
它还没有,因此不调用update_comment_meta()就执行返回。 To work properly the nonce field should be set in extend_comment_meta_box() like this:
为了正常工作,应在extend_comment_meta_box()中设置nonce字段,如下所示:
wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );
Discussion of the nonce field is here: https://codex.wordpress.org/Function_Reference/wp_nonce_field 关于随机数字段的讨论在这里: https : //codex.wordpress.org/Function_Reference/wp_nonce_field
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.