Using WordPress ‘delete_link’ PHP action

The delete_link WordPress PHP action fires before a link is deleted.

Usage

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

    return $link_id;
}

Parameters

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

More information

See WordPress Developer Resources: delete_link

Examples

Log link deletion events for debugging purposes.

add_action('delete_link', 'log_link_deletion');
function log_link_deletion($link_id) {
    // Log the link deletion event
    error_log("Link with ID {$link_id} was deleted.");
}

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

add_action('delete_link', 'send_email_on_link_deletion');
function send_email_on_link_deletion($link_id) {
    // Send email notification to the site administrator
    $admin_email = get_option('admin_email');
    $subject = "Link Deleted on Your WordPress Site";
    $message = "A link with ID {$link_id} has been deleted from your website.";

    wp_mail($admin_email, $subject, $message);
}

Delete any metadata associated with the link when the link is deleted.

add_action('delete_link', 'remove_link_metadata');
function remove_link_metadata($link_id) {
    // Remove metadata associated with the link
    delete_metadata('link', $link_id, '');
}

Prevent links with a specific term from being deleted.

add_action('delete_link', 'prevent_link_deletion', 10, 1);
function prevent_link_deletion($link_id) {
    // Check if the link has a specific term
    $term = 'restricted-term';
    if (has_term($term, 'link_category', $link_id)) {
        wp_die("The link with ID {$link_id} cannot be deleted as it is tagged with the restricted term '{$term}'.");
    }
}

Update a custom counter

Update a custom counter when a link is deleted.

add_action('delete_link', 'update_link_counter');
function update_link_counter($link_id) {
    // Update the custom link counter
    $counter = get_option('link_deletion_counter');
    $counter = is_numeric($counter) ? $counter + 1 : 1;
    update_option('link_deletion_counter', $counter);
}