我正在使用Wordpress(版本4.5.3),并在注释表单中添加了自定义字段。该字段是使用选择标签的下拉列表。该字段显示在表单中很好,我可以选择一个选项,该选项的值可以正确保存。无法在Wordpress中编辑注释元数据
我还添加了编辑编辑注释页面中的自定义字段的代码(/wordpress/wp-admin/comment.php?action=editcomment)。我能够检索存储的值,然后重新选择具有适当值的下拉列表。
我已经添加了代码来保存自定义字段的编辑值,但这是行不通的。当从编辑注释页面,我从下拉列表中选择一个不同的值,然后点击“更新”按钮,新选择的值不会被保存。
我正在添加代码来完成所有这些在functions.php中。以下是代码字段添加到窗体和存储数据:
// 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);
}
这里是一个自定义字段添加到注释编辑页面的代码:
// 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
}
这一切似乎是工作精细。以下是保存自定义字段的编辑值的代码:
// 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;
}
此代码不起作用。当我返回编辑评论页面时,我看到了自定义字段的原始值,而不是编辑的值。
为什么没有保存编辑的值?