#19: delete_{$meta_type}_metadata

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:

  1. bool null If anything other than null is returned, delete_metadata() will short-circuit and return the boolean equivalent of that value. Default return for delete_metadata() is true on successful delete, false on failure
  2. int $object_id The user, comment or post object id
  3. string $meta_key The meta key to delete
  4. string $meta_value The meta value to delete
  5. bool $delete_all Whether to ignore $object_id further down in delete_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.