The delete_{$meta_type}_metadata dynamic filter effectively allows you to prevent certain meta data from being deleted. The dynamic $meta_type variable accepts ‘user’, ‘comment’, or ‘post’.
It is evaluated in delete_metadata() in the wp-includes/meta.php file.
delete_{$meta_type}_metadata accepts 5 arguments:
- bool
nullIf anything other than null is returned,delete_metadata()will short-circuit and return the boolean equivalent of that value. Default return fordelete_metadata()is true on successful delete, false on failure - int
$object_idThe user, comment or post object id - string
$meta_keyThe meta key to delete - string
$meta_valueThe meta value to delete - bool
$delete_allWhether to ignore$object_idfurther down indelete_metadata(). Default is false
Example:
Let’s say you want to keep non-admin users from deleting featured images once they’re set (for some reason). The following uses the dynamic delete_post_metadata hook accomplish this.
View the code example on Gist.