get_delete_post_link()WP 2.9.0

Gets the link (URL) to delete a post.

Can be used to move a post to the trash, delete pages, posts, attachments, and revisions.

If the user does not have sufficient permissions to delete the post, it will return an empty string.

Hooks from the function

Returns

String|null.

Usage

get_delete_post_link( $id, $deprecated, $force_delete );
$id(integer) (required)
ID of the post.
$deprecated
The parameter is deprecated since version 3.0.
Default: ''
$force_delete(boolean)
true — will delete the post bypassing the trash.
Default: false

Examples

0

#1 Demo Sample

echo get_delete_post_link( 1 );

// http://example.com/wp-admin/post.php?post=1&action=delete&_wpnonce=d21da8cad3
0

#2 Full link to delete the post:

echo '<a href="'. get_delete_post_link() .'">delete post</a>';
0

#3 Display the link only if the user has enough privilege for deletion:

if( current_user_can( 'delete_posts' ) ) {
	echo '<a href="'. get_delete_post_link( $post->ID) .'">Delete without restoring</a>';
}

Changelog

Since 2.9.0 Introduced.

get_delete_post_link() code WP 6.9.1

function get_delete_post_link( $post = 0, $deprecated = '', $force_delete = false ) {
	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __FUNCTION__, '3.0.0' );
	}

	$post = get_post( $post );

	if ( ! $post ) {
		return;
	}

	$post_type_object = get_post_type_object( $post->post_type );

	if ( ! $post_type_object ) {
		return;
	}

	if ( ! current_user_can( 'delete_post', $post->ID ) ) {
		return;
	}

	$action = ( $force_delete || ! EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';

	$delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );

	/**
	 * Filters the post delete link.
	 *
	 * @since 2.9.0
	 *
	 * @param string $link         The delete link.
	 * @param int    $post_id      Post ID.
	 * @param bool   $force_delete Whether to bypass the Trash and force deletion. Default false.
	 */
	return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
}