Using WordPress ‘deleted_link’ PHP action

The deleted_link WordPress PHP action fires after a link has been deleted. It provides the ID of the deleted link.

Usage

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

Parameters

  • $link_id (int) – ID of the deleted link.

More information

See WordPress Developer Resources: deleted_link

Examples

Log the ID of deleted links to a custom log file.

add_action('deleted_link', 'log_deleted_links');
function log_deleted_links($link_id) {
  // Log the deleted link ID to a custom log file
  error_log("Deleted link ID: {$link_id}", 3, '/path/to/your/logs/deleted_links.log');
}

Send Email Notification

Send an email notification to the admin when a link is deleted.

add_action('deleted_link', 'send_link_deletion_notification');
function send_link_deletion_notification($link_id) {
  $to = '[email protected]';
  $subject = 'Link Deleted';
  $message = "A link with ID {$link_id} has been deleted.";
  wp_mail($to, $subject, $message);
}

Update Custom Post Meta

Update the custom post meta when a link is deleted.

add_action('deleted_link', 'update_custom_post_meta_on_link_deletion');
function update_custom_post_meta_on_link_deletion($link_id) {
  // Get posts that have the deleted link ID as meta value
  $args = array(
    'post_type' => 'your_post_type',
    'meta_query' => array(
      array(
        'key' => 'your_meta_key',
        'value' => $link_id,
        'compare' => '=',
      ),
    ),
  );
  $posts = get_posts($args);

  // Loop through the posts and update the custom post meta
  foreach ($posts as $post) {
    update_post_meta($post->ID, 'your_meta_key', 'new_value');
  }
}

Remove the deleted link from a custom table in the database.

add_action('deleted_link', 'remove_deleted_link_from_custom_table');
function remove_deleted_link_from_custom_table($link_id) {
  global $wpdb;

  // Delete the record from the custom table
  $wpdb->delete('your_custom_table', array('link_id' => $link_id));
}

Add Custom Notification Message

Display a custom notification message when a link is deleted.

add_action('deleted_link', 'add_custom_notification_message');
function add_custom_notification_message($link_id) {
  // Add the custom notification message
  add_action('admin_notices', function () use ($link_id) {
    echo '<div class="notice notice-success is-dismissible">';
    echo "<p>A link with ID {$link_id} has been deleted.</p>";
    echo '</div>';
  });
}