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

How to use delete_meta_type_meta action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
December 22, 2022
5 minutes read

delete_meta_type_meta action

The dynamic portion of the hook name, $meta_type, refers to the meta object type (post, comment, term, user, or any other type with an associated meta table).

To use delete_meta_type_meta action, first, you have to register it using add_action. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function execute_on_delete_meta_type_meta_event which takes 4 parameters, and we registered it using add_action. The first parameter delete_meta_type_meta is the name of the hook. The second parameter execute_on_delete_meta_type_meta_event is the name of the function which needs to be called. The third parameter is the priority of calling the hook if the same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook, so you can use remove_action to remove delete_meta_type_meta action.

Parameters

    Below are the 4 parameters required to use this hook.

  • $meta_ids : (string[]) An array of metadata entry IDs to delete.
  • $object_id : (int) ID of the object metadata is for.
  • $meta_key : (string) Metadata key.
  • $_meta_value : (mixed) Metadata value.

Live Example

do_action( "delete_{$meta_type}_meta", string[] $meta_ids, int $object_id, string $meta_key, mixed $_meta_value )

Below is an example of how you can use this hook.

    function weplugins_execute_on_delete_meta_type_meta_event($meta_ids, $object_id, $meta_key, $_meta_value){
      //You can write code here to be executed when this action occurs in WordPress. Use the parameters received in the function arguments & implement the required additional custom functionality according to your website requirements.
    }
    // add the action
    add_action( "delete_meta_type_meta", "weplugins_execute_on_delete_meta_type_meta_event" , 10, 4);
  

Example 1: Logging Deleted Meta Data

In this example, we log the deleted meta data for debugging purposes.

    function weplugins_log_deleted_meta_data($meta_ids, $object_id, $meta_key, $_meta_value) {
        error_log("Meta data deleted: " . print_r(compact('meta_ids', 'object_id', 'meta_key', '_meta_value'), true));
    }
    add_action("delete_meta_type_meta", "weplugins_log_deleted_meta_data", 10, 4);
  

Example 2: Clearing Cache on Meta Deletion

This example clears the cache whenever specific meta data is deleted.

    function weplugins_clear_cache_on_meta_deletion($meta_ids, $object_id, $meta_key, $_meta_value) {
        if ($meta_key === 'my_special_meta_key') {
            wp_cache_delete($object_id, 'my_custom_cache_group');
        }
    }
    add_action("delete_meta_type_meta", "weplugins_clear_cache_on_meta_deletion", 10, 4);
  

Example 3: Notifying Admin on Meta Deletion

In this example, an admin is notified via email whenever certain meta data is deleted.

    function weplugins_notify_admin_on_meta_deletion($meta_ids, $object_id, $meta_key, $_meta_value) {
        if ($meta_key === 'notify_admin_meta_key') {
            wp_mail('admin@example.com', 'Meta Data Deleted', "Meta data with key $meta_key was deleted.");
        }
    }
    add_action("delete_meta_type_meta", "weplugins_notify_admin_on_meta_deletion", 10, 4);
  

To remove a hook callback, use the example below.

remove_action( "delete_meta_type_meta", "weplugins_execute_on_delete_meta_type_meta_event", 10, 4 );

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you need any customization, please feel free to contact us.

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.