Function and usage of get_delete_post_link() in wordpress


Answers ( 1 )


    The get_delete_post_link() function in WordPress is a useful tool for theme and plugin developers. It generates a link to delete a specific post, and can be used within or outside the WordPress Loop. Here's a detailed explanation of the function and its usage:


    • Name: get_delete_post_link()


    • Retrieves a delete link (URL) for a specified post. This can be used to create a direct link for users to delete a post. It's useful for front-end interfaces where you might want to provide a quick way to delete posts.


    1. $post (int|WP_Post - Optional):

      • Type: Integer or WP_Post object.
      • Description: The ID of the post or the post object itself.
      • Default: Global $post variable, which refers to the current post in a standard WordPress loop.
    2. $deprecated (string - Optional):

      • Type: String.
      • Description: This parameter is not used and is included for backward compatibility.
      • Default: Empty string ''.
    3. $force_delete (bool - Optional):

      • Type: Boolean.
      • Description: Determines whether the post should be deleted permanently or moved to the Trash.
      • Default: false. If set to true, the post is deleted permanently; if false, the post is moved to Trash (if the Trash feature is enabled).


    • Type: string|void
    • Description: Returns the URL for deleting the specified post. If the user does not have the required permission to delete the post, or if the post does not exist, the function returns void.

    Sample Usage:

    if ( have_posts() ) {
        while ( have_posts() ) {
            // Example usage inside a loop
            $delete_link = get_delete_post_link(get_the_ID());
            echo '<a href="' . esc_url( $delete_link ) . '" onclick="return confirm(\'Are you sure you want to delete this?\')">Delete Post</a>';
    // Example usage outside the loop for a specific post ID
    $post_id = 123; // Replace 123 with the actual post ID
    $delete_link = get_delete_post_link($post_id, '', true); // Force delete
    if ( $delete_link ) {
        echo '<a href="' . esc_url( $delete_link ) . '" onclick="return confirm(\'Are you sure you want to delete this?\')">Delete Post</a>';

    In this example, get_delete_post_link() is used to generate a delete link for each post within the loop, and for a specific post outside the loop. The esc_url() function is used to escape the URL for output to prevent XSS attacks, and a JavaScript confirm function is added to prompt the user for confirmation before deleting.

Leave an answer