Home WordPress Links Functions get_delete_post_link() WordPress get_delete_post_link() Function Definition In WordPress, get_delete_post_link() returns a URL that moves a post to the Trash. With one option it can delete the post for good.
The URL has a security code (a nonce ), so it cannot be guessed. If the current user is not allowed to delete the post, the function returns nothing. Always check the result before you print a link.
Syntax Get the URL, check it, then print a link.
<?php $url = get_delete_post_link(); ?> Call What it does get_delete_post_link()Trash link for the current post. get_delete_post_link( 42 )Trash link for post 42. get_delete_post_link( 42, '', true )Link that deletes post 42 permanently.
It returns a URL. It does not delete anything by itself. The post is only removed when someone opens the link.
Full declaration get_delete_post_link( int|WP_Post $post = 0, string $deprecated = '', bool $force_delete = false ): string|void Parameters Parameter Description $postint|WP_Post. The post ID or object. Default is 0, which means the current post. $deprecatedstring. Not used anymore. Pass an empty string. $force_deletebool. If true, the link deletes the post permanently. If false (default), it moves the post to the Trash.
Return Value Return Value Description string The delete URL, with a nonce. nothing (void) If the post does not exist or the user cannot delete it.
WordPress Examples Three short examples. Each one shows the code and what it prints or does.
1. Get the trash URL 2. Delete permanently with force_delete 3. Print a Move to Trash link Example 1 — Get the trash URL Print the URL of the current post for an admin.
<?php
$url = get_delete_post_link();
if ( $url ) {
echo esc_url( $url );
} https://example.com/wp-admin/post.php?post=42&action=trash&_wpnonce=4f6b2c8d1e How It Works get_delete_post_link() builds a trash URL for the current post.The check makes sure there is a URL (visitors get nothing). The _wpnonce part is the security code. Example 2 — Delete permanently with force_delete Set the third argument to true to skip the Trash.
<?php
$url = get_delete_post_link( 42, '', true );
if ( $url ) {
echo esc_url( $url );
} https://example.com/wp-admin/post.php?post=42&action=delete&_wpnonce=9a7c3d5b21 How It Works 42 is the post ID.The empty string is the unused second argument. true changes the action from trash to delete.Example 3 — Print a Move to Trash link Build a full link, shown only to users who can delete the post.
<?php
$url = get_delete_post_link();
if ( $url ) {
echo '<a href="' . esc_url( $url ) . '">Move to Trash</a>';
} <a href="https://example.com/wp-admin/post.php?post=42&action=trash&_wpnonce=4f6b2c8d1e">Move to Trash</a> How It Works The URL is only a string, so we build the link ourselves. esc_url() escapes it for HTML.A visitor sees no link, because $url is empty. Frequently Asked Questions What does get_delete_post_link() do? It returns a secure URL that moves a post to the Trash, or deletes it permanently if you ask for that.
Does it delete the post when I call it? No. It only builds the URL. The post is removed when someone opens the link.
Why does it return nothing? The post may not exist, or the current user may not have permission to delete it.
What is the _wpnonce in the URL? It is a one-time security code. It protects the link from being used by someone else.
How do I skip the Trash? Pass true as the third argument. Use it carefully, because the post cannot be restored.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion