Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use deleted_postmeta action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
May 20, 2023
5 minutes read

Ever wondered what happens behind the scenes when you delete metadata for a post in WordPress? Well, that’s where the deleted_postmeta action hook comes into play. This hook is fired right after the post metadata is deleted, giving you the opportunity to run some custom code. Let’s dive into how you can use it with some live examples!

Example 1: Logging Deleted Metadata IDs

In this example, we’ll log the IDs of deleted metadata into a custom file for future reference. This can be especially useful for debugging or tracking changes.

    function weplugins_log_deleted_meta_ids($meta_ids){
       file_put_contents('deleted_meta_log.txt', implode(',', $meta_ids) . PHP_EOL, FILE_APPEND);
    }
    add_action("deleted_postmeta", "weplugins_log_deleted_meta_ids", 10, 1);
    

Example 2: Notify Admin on Metadata Deletion

Here, we send an email notification to the site admin whenever metadata is deleted, keeping them informed of such actions.

    function weplugins_notify_admin_on_meta_delete($meta_ids){
       $admin_email = get_option('admin_email');
       $subject = 'Metadata Deleted';
       $message = 'The following metadata IDs have been deleted: ' . implode(', ', $meta_ids);
       wp_mail($admin_email, $subject, $message);
    }
    add_action("deleted_postmeta", "weplugins_notify_admin_on_meta_delete", 10, 1);
    

Example 3: Custom Cleanup After Metadata Deletion

Use this example if you need to perform additional cleanup tasks once metadata is deleted. It might involve clearing cache or triggering external APIs.

    function weplugins_custom_cleanup_after_meta_delete($meta_ids){
       // Custom cleanup code here
    }
    add_action("deleted_postmeta", "weplugins_custom_cleanup_after_meta_delete", 10, 1);
    

Access Premium WordPress Plugins

If you need any customization or run into issues with the deleted_postmeta hook, feel free to Contact Us. Our team at WePlugins is here to help!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.