参考带有链接的帖子“添加自定义字段bbpress主题表单”https://wp-dreams.com/articles/2013/06/adding-custom-fields-bbpress-topic-form/,我想相应地向bbpress回复表单添加自定义字段。我只把几个关键词从“主题”改为“回复”。我还测试了更多的钩子来触发函数。但是,在测试了测试回复后,我无法显示相关字段。代码如下:
add_action ( \'bbp_theme_before_reply_form_content\', \'bbp_extra_fields\');
function bbp_extra_fields() {
$value = get_post_meta( bbp_get_reply_id(), \'bbp_extra_field1\', true);
echo \'<label for="bbp_extra_field1">Extra Field 1</label><br>\';
echo "<input type=\'text\' name=\'bbp_extra_field1\' value=\'".$value."\'>";
$value = get_post_meta( bbp_get_reply_id(), \'bbp_extra_field2\', true);
echo \'<label for="bbp_extra_field1">Extra Field 2</label><br>\';
echo "<input type=\'text\' name=\'bbp_extra_field2\' value=\'".$value."\'>";
}
// I can find the fields added (two input boxes)
add_action ( \'bbp_new_reply\', \'bbp_save_extra_fields\', 10, 1 );
add_action ( \'bbp_edit_reply\', \'bbp_save_extra_fields\', 10, 1 );
add_action ( \'save_post\', \'bbp_save_extra_fields\', 10, 1 );
function bbp_save_extra_fields($reply_id=0) {
if (isset($_POST) && $_POST[\'bbp_extra_field1\']!=\'\')
update_post_meta( $reply_id, \'bbp_extra_field1\', $_POST[\'bbp_extra_field1\'] );
if (isset($_POST) && $_POST[\'bbp_extra_field1\']!=\'\')
update_post_meta( $reply_id, \'bbp_extra_field1\', $_POST[\'bbp_extra_field2\'] );
}
//here I am not sure which hook should be used accordingly, but all four hooks do not work
add_action(\'bbp_template_before_replies_loop\', \'bbp_show_extra_fields\');
add_action(\'bbp_new_reply\', \'bbp_show_extra_fields\');
add_action(\'bbp_edit_reply\', \'bbp_show_extra_fields\');
add_action(\'save_post\', \'bbp_show_extra_fields\');
function bbp_show_extra_fields() {
$reply_id = bbp_get_reply_id();
$value1 = get_post_meta( $reply_id, \'bbp_extra_field1\', true);
$value2 = get_post_meta( $reply_id, \'bbp_extra_field2\', true);
//finally I can not find any extra fields information after I sent the reply
echo "Field 1: ".$value1."<br>";
echo "Field 2: ".$value2."<br>";
}
请给出建议。谢谢