I have a custom post type that must have its feature image. Its feature image also appears in the Media Library where user can delete the image/attachment file permanently. But I want to prevent user from deleting the feature image of my custom post type. So I use the following hook to intercept the ajax request, validate if user is deleting the image of my custom post type and stop the process by using wp_die()
.
add_filter('pre_delete_attachment', 'check_my_custom_post_type', 0, 2);
function check_my_custom_post_type($delete, $post) {
if (Yes it is image of my custom type) {
wp_die('My message', 'My title', ['response' => 400]);
}
}
It works fine on the server side. The image of my custom post type cannot be deleted. But Media Library, on the client side, still removes the image from its views even the image on the server side has not been deleted and an exception 400 has been thrown.
How to prevent Media Library from removing images from its views on the client side if image has not been deleted on the server side?
2
Answers
In documentation second parameter is
bool|null
. Try toreturn false
instead ofwp_die
You can remove the delete link totally using the
wp_prepare_attachment_for_js
hook.This works also with the "Bulk Delete" action. If the attachment has this
nonce
valuefalse
, it will not be deleted with the other bulk selected attachments 👍🏻.But this works only with the Grid View that uses the
wp_prepare_attachment_for_js
hook. The List view doesn’t use any Javascript. So, to prevent deleting media files in the List view, you have to:media_row_actions
hook.bulk_actions-{$screen}
hook.