Skip to content

Instantly share code, notes, and snippets.

@WerdsWords
Created July 25, 2013 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WerdsWords/6077012 to your computer and use it in GitHub Desktop.
Save WerdsWords/6077012 to your computer and use it in GitHub Desktop.
#19: delete_{$meta_type}_metadata
<?php
/**
* Keep posts' featured images
*
* @see delete_metadata()
*
* @param bool $bool Default is null, anything else short-circuits delete_metadata().
* @param int $object_id The post, user, or comment object id.
* @param string $meta_key The meta key to delete.
* @param string $meta_value The meta value to delete.
* @param bool $delete_all Whether to delete all regardless of $object_id in delete_metadata(). Default is false.
*
* @return bool|null Null to continue, true for success, false for failure.
*/
function wpdocs_keep_featured_images( $bool, $object_id, $meta_key, $meta_value, $delete_all ) {
// If user isn't an admin and is trying to remove the featured image key
if ( ! current_user_can( 'manage_options' ) && '_thumbnail_id' == $meta_key )
return false;
return null;
}
add_filter( 'delete_post_metadata', 'wpdocs_keep_featured_images', 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment