Using WordPress ‘delete_{$meta_type}meta’ PHP action

The delete_{$meta_type}meta WordPress action fires immediately before deleting metadata of a specific type for a post, comment, term, or user.

Usage

add_action('delete_postmeta', 'your_custom_function');
function your_custom_function($meta_id) {
    // your custom code here
}

Parameters

  • $meta_id (int): ID of the metadata entry to delete.

More information

See WordPress Developer Resources: delete_{$meta_type}meta

Examples

Log deleted post metadata

Logs deleted post metadata to a custom log file.

add_action('delete_postmeta', 'log_deleted_postmeta');
function log_deleted_postmeta($meta_id) {
    // Get metadata by ID
    $meta = get_metadata_by_mid('post', $meta_id);

    // Create log entry
    $log_entry = "Deleted post meta: ID {$meta_id}, key {$meta->meta_key}, value {$meta->meta_value}" . PHP_EOL;

    // Append to log file
    file_put_contents('postmeta_deletion.log', $log_entry, FILE_APPEND);
}

Prevent deletion of specific post metadata

Prevents deletion of a specific post metadata key.

add_action('delete_postmeta', 'prevent_specific_postmeta_deletion', 10, 1);
function prevent_specific_postmeta_deletion($meta_id) {
    // Define metadata key to protect
    $protected_key = 'protected_meta_key';

    // Get metadata by ID
    $meta = get_metadata_by_mid('post', $meta_id);

    // Check if it's the protected key and cancel deletion
    if ($meta->meta_key === $protected_key) {
        wp_die('You cannot delete this protected metadata.');
    }
}

Notify admin on comment metadata deletion

Sends an email notification to the admin when comment metadata is deleted.

add_action('delete_commentmeta', 'notify_admin_on_commentmeta_deletion');
function notify_admin_on_commentmeta_deletion($meta_id) {
    // Get metadata by ID
    $meta = get_metadata_by_mid('comment', $meta_id);

    // Prepare email content
    $subject = "Comment Metadata Deleted";
    $message = "Comment metadata with ID {$meta_id} has been deleted. Key: {$meta->meta_key}, Value: {$meta->meta_value}";

    // Send email to admin
    wp_mail(get_option('admin_email'), $subject, $message);
}

Deletes additional custom data related to the deleted term metadata.

add_action('delete_termmeta', 'clean_up_related_data_on_termmeta_deletion');
function clean_up_related_data_on_termmeta_deletion($meta_id) {
    // Get metadata by ID
    $meta = get_metadata_by_mid('term', $meta_id);

    // Check if it's the custom key and clean up related data
    if ($meta->meta_key === 'custom_term_meta_key') {
        delete_option("custom_option_{$meta_id}");
    }
}

Increment user metadata deletion counter

Increments a counter for the number of times user metadata has been deleted.

add_action('delete_usermeta', 'increment_usermeta_deletion_counter');
function increment_usermeta_deletion_counter($meta_id) {
    // Get current counter value
    $counter = get_option('usermeta_deletion_counter', 0);

    // Increment and update counter
    $counter++;
    update_option('usermeta_deletion_counter', $counter);
}