Using WordPress ‘get_delete_post_link’ PHP filter

The get_delete_post_link WordPress PHP filter allows you to modify the delete link for a specific post.

Usage

add_filter('get_delete_post_link', 'your_custom_function', 10, 3);

function your_custom_function($link, $post_id, $force_delete) {
    // your custom code here
    return $link;
}

Parameters

  • $link (string) – The delete link.
  • $post_id (int) – Post ID.
  • $force_delete (bool) – Whether to bypass the Trash and force deletion. Default false.

More information

See WordPress Developer Resources: get_delete_post_link

Examples

Modify the delete link to use a custom URL.

add_filter('get_delete_post_link', 'change_delete_post_link', 10, 3);

function change_delete_post_link($link, $post_id, $force_delete) {
    return "https://example.com/delete-post/{$post_id}";
}

Add a JavaScript confirmation message when clicking the delete link.

add_filter('get_delete_post_link', 'add_confirm_message', 10, 3);

function add_confirm_message($link, $post_id, $force_delete) {
    return $link . "\" onclick=\"return confirm('Are you sure you want to delete this post?');";
}

Force delete for specific post types

Force delete for posts with a specific custom post type.

add_filter('get_delete_post_link', 'force_delete_custom_post_type', 10, 3);

function force_delete_custom_post_type($link, $post_id, $force_delete) {
    $post_type = get_post_type($post_id);

    if ('custom_post_type' === $post_type) {
        $force_delete = true;
    }
    return $link;
}

Modify the delete link for posts with a specific tag.

add_filter('get_delete_post_link', 'change_delete_link_for_tagged_posts', 10, 3);

function change_delete_link_for_tagged_posts($link, $post_id, $force_delete) {
    if (has_tag('special-tag', $post_id)) {
        return "https://example.com/special-delete/{$post_id}";
    }
    return $link;
}

Disable the delete link for posts created by authors with a specific user role.

add_filter('get_delete_post_link', 'disable_delete_link_for_user_role', 10, 3);

function disable_delete_link_for_user_role($link, $post_id, $force_delete) {
    $post_author = get_post_field('post_author', $post_id);
    $user = get_user_by('id', $post_author);

    if (in_array('specific_role', $user->roles)) {
        return '';
    }
    return $link;
}

Tagged in

2 comments on “Using WordPress ‘get_delete_post_link’ PHP filter

  1. Hello Adrian,

    I couldn’ find a viable solution, so I tried another approach: to redirect 404 page to custom frontend editor dashboard which I built for the Editor role user. Here’s the code:

    function editor_redirect_404() {
    global $wp_query;
    if ( $wp_query->is_404 ) {
    wp_redirect( home_url( ‘/dashboard/’ ) );
    exit;
    }
    }
    add_action(‘template_redirect’, ‘editor_redirect_404’, 1);

    I don’t want the visitors of the site to experience this (they have regular 404 page), so this redirection is applied only if user is logged in and have the Editor role. This is achieved by using condition builder in WPCodeBox plugin.

  2. Hi Adrian,

    I found your article while I tried to find a solution to particular problem: I’m using get_delete_post_link to delete custom post from the front-end, but after deletion I get 404 page. How can I redirect to homepage after custom post is deleted? I’d really appreciate your help. I inserted in functions.php this code:

    function wp_delete_post_link($link = ‘Obriši’, $before = ”, $after = ”) {
    global $post;
    $link = “ID, ‘delete-post_’ . $post->ID) . “‘>”.$link.”“;
    echo $before . $link . $after;
    }

    , and after that I created shortcode to generate delete button:

    function wpc_elementor_shortcode( $atts ) {
    wp_delete_post_link();
    }
    add_shortcode( ‘my_shortcode’, ‘wpc_elementor_shortcode’);

    Can you help me to find a solution which I can incorporate in this code?

Leave a Reply to Alex Cancel reply

Your email address will not be published. Required fields are marked *