在评论编辑屏幕中插入元框与在帖子类型屏幕中插入元框相同。但它只能放在宽栏上,侧边栏似乎不接受其他框。
为了捕获正在发布的数据,我只找到了过滤器comment_edit_redirect
.
这必须进行调整才能与插件一起使用。本例中的自定义字段为meta_comment_field
. 请参见代码注释:
// SAVE COMMENT META
// only found this hook to process the POST
add_filter( \'comment_edit_redirect\', \'save_comment_wpse_82317\', 10, 2 );
// META BOX
add_action( \'add_meta_boxes\', \'add_custom_box_wpse_82317\' );
/**
* Save Custom Comment Field
* This hook deals with the redirect after saving, we are only taking advantage of it
*/
function save_comment_wpse_82317( $location, $comment_id )
{
// Not allowed, return regular value without updating meta
if ( !wp_verify_nonce( $_POST[\'noncename_wpse_82317\'], plugin_basename( __FILE__ ) )
&& !isset( $_POST[\'meta_comment_field\'] )
)
return $location;
// Update meta
update_comment_meta(
$comment_id,
\'meta_comment_field\',
sanitize_text_field( $_POST[\'meta_comment_field\'] )
);
// Return regular value after updating
return $location;
}
/**
* Add Comment meta box
*/
function add_custom_box_wpse_82317()
{
add_meta_box(
\'section_id_wpse_82317\',
__( \'Meta Comment Meta\' ),
\'inner_custom_box_wpse_82317\',
\'comment\',
\'normal\'
);
}
/**
* Render meta box with Custom Field
*/
function inner_custom_box_wpse_82317( $comment )
{
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'noncename_wpse_82317\' );
$c_meta = get_comment_meta( $comment->comment_ID, \'meta_comment_field\', true );
echo "<input type=\'text\' id=\'meta_comment_field\' name=\'meta_comment_field\' value=\'",
esc_attr( $c_meta ),
"\' size=\'25\' />";
}