Using WordPress ‘get_delete_post_link()’ PHP function

The get_delete_post_link() WordPress PHP function retrieves the delete posts link for a post. It can be used within the WordPress loop or outside of it, with any post type.

Usage

get_delete_post_link($post, $deprecated = '', $force_delete = false);

Custom Example:

echo get_delete_post_link(123, '', true);

Output:

https://example.com/wp-admin/post.php?post=123&action=delete&_wpnonce=87d5108013

Parameters

  • $post (int|WP_Post): Optional. Post ID or post object. Default is the global $post.
  • $deprecated (string): Optional. Not used. Default: ”.
  • $force_delete (bool): Optional. Whether to bypass Trash and force deletion. Default: false.

More information

See WordPress Developer Resources: get_delete_post_link

Important: If you use this function with a custom post type, you must define the “show_ui” parameter of the register_post_type() function to true. With “show_ui” parameter to false, the link doesn’t work.

Examples

Display delete link in post meta

Display the delete post link in the post meta section on a single post page.

if (is_single()) {
  echo '<div class="post-meta">';
  echo 'Delete post: <a href="' . get_delete_post_link() . '">Delete</a>';
  echo '</div>';
}

Delete link for a custom post type

Display the delete link for a custom post type ‘event’.

$event_post = get_post(456);
if ($event_post->post_type == 'event') {
  echo 'Delete event: <a href="' . get_delete_post_link($event_post->ID) . '">Delete</a>';
}

Force delete a post

Display a delete link that bypasses the Trash and force deletes the post.

$post_id = 789;
echo 'Force delete post: <a href="' . get_delete_post_link($post_id, '', true) . '">Delete</a>';

Display a delete link with a JavaScript confirmation dialog.

$delete_link = get_delete_post_link();
echo 'Delete post: <a href="' . $delete_link . '" onclick="return confirm(\'Are you sure you want to delete this post?\');">Delete</a>';

Display delete link only for post author

Show the delete link only if the current user is the post author.

$current_user = wp_get_current_user();
$post_author_id = get_post_field('post_author', get_the_ID());
if ($current_user->ID == $post_author_id) {
  echo 'Delete your post: <a href="' . get_delete_post_link() . '">Delete</a>';
}