admin管理员组文章数量:1289869
I have added a custom field "Buy Now" link on media edit page but I am not able to save that data like other post types. Is there any other hook which is triggered after add/edit/update media/attachment?
I am using this function:
function update_attachment_extra_info( $post_id ) {
// code to update data
}
add_action( 'save_post', 'update_attachment_extra_info' );
But this hook is not triggered.
I have added a custom field "Buy Now" link on media edit page but I am not able to save that data like other post types. Is there any other hook which is triggered after add/edit/update media/attachment?
I am using this function:
function update_attachment_extra_info( $post_id ) {
// code to update data
}
add_action( 'save_post', 'update_attachment_extra_info' );
But this hook is not triggered.
Share Improve this question edited Feb 14, 2017 at 21:58 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked Feb 14, 2017 at 17:59 shishir mishrashishir mishra 4065 silver badges10 bronze badges 4 |1 Answer
Reset to default 6Here is an example that adds a custom media field named Buy Now. This example saves the value of the custom field on the media overlay screen via ajax, as well as the media edit screen (non ajax).
Edit: Added nonce check and sanitization.
/**
* Add custom field to media.
*/
add_filter( 'attachment_fields_to_edit', 'wpse256463_attachment_fields', 10, 2 );
function wpse256463_attachment_fields( $fields, $post ) {
$meta = get_post_meta( $post->ID, 'buy_now', true );
$fields['buy_now'] = [
'label' => __( 'Buy Now', 'text-domain' ),
'input' => 'text',
'value' => $meta,
'show_in_edit' => true,
'extra_rows' => [
'nonce' => wp_nonce_field(
'update_attachment_buy_now', // Action.
'nonce_attachment_buy_now', // Nonce name.
true, // Output referer?
false // Echo?
),
],
];
return $fields;
}
/**
* Update custom field within media overlay (via ajax).
*/
add_action( 'wp_ajax_save-attachment-compat', 'wpse256463_media_fields', 0, 1 );
function wpse256463_media_fields() {
$nonce = $_REQUEST['nonce_attachment_buy_now'] ?? false;
// Bail if the nonce check fails.
if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'update_attachment_buy_now' ) ) {
return;
}
// Bail if the post ID is empty.
$post_id = intval( $_POST['id'] );
if ( empty( $post_id ) ) {
return;
}
// Update the post.
$meta = $_POST['attachments'][ $post_id ]['buy_now'] ?? '';
$meta = wp_kses_post( $meta );
update_post_meta( $post_id, 'buy_now', $meta );
clean_post_cache( $post_id );
}
/**
* Update media custom field from edit media page (non ajax).
*/
add_action( 'edit_attachment', 'wpse256463_update_attachment_meta', 1 );
function wpse256463_update_attachment_meta( $post_id ) {
$nonce = $_REQUEST['nonce_attachment_buy_now'] ?? false;
// Bail if the nonce check fails.
if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'update_attachment_buy_now' ) ) {
return;
}
$buy_now = isset( $_POST['attachments'][ $post_id ]['buy_now'] )
? wp_kses_post( $_POST['attachments'][ $post_id ]['buy_now'] )
: false;
update_post_meta( $post_id, 'buy_now', $buy_now );
return;
}
本文标签: How to save custom fields for attachments
版权声明:本文标题:How to save custom fields for attachments 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741412155a2377298.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
attachment_fields_to_edit
,edit_attachment
, andwp_ajax_save-attachment-compat
are what you'll need. The code from this question works, and can easily be adapted: wordpress.stackexchange/questions/180092/… – Dave Romsey Commented Feb 14, 2017 at 21:24