This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
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);
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!
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.