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?
In documentation second parameter is
bool|null
. Try toreturn false
instead ofwp_die