Title: Delete Post Link
Last modified: August 19, 2016

---

# Delete Post Link

 *  [acondiff](https://wordpress.org/support/users/acondiff/)
 * (@acondiff)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/delete-post-link/)
 * This function which adds a Delete link to the admin should work but it doesn’t.
 *     ```
       function delete_post_link()
       {
       	global $post;
       	echo "<a href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?post%3D" . $post->ID . "&action%3Ddelete&", 'delete-post_' . $post->ID) . "'>Delete</a>";
       }
       ```
   
 * I get this:
    Are you sure you want to do this? Please try again.
 * I think it has to do with wp_nonce_url. Any ideas?
 * Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)

 *  [kristarella](https://wordpress.org/support/users/kristarella/)
 * (@kristarella)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/delete-post-link/#post-1587911)
 * I second this question.
 * I’ve tried
    `href="' . get_bloginfo("url") . '/wp-admin/post.php?post=' . $post-
   >ID . '&action=trash"` and `href="' . wp_nonce_url( get_bloginfo("url") . "/wp-
   admin/post.php?post=" . $post->ID . "&action=trash") . '"` Both of which bring
   me to the same failure notice page with a link back to the page I was just on
   saying “Please try again”.
 * The link structure of action=trash is slightly different to acondiff, above; 
   I changed mine to replicate the link structure in the WP admin.
 *  [adanahmad](https://wordpress.org/support/users/adanahmad/)
 * (@adanahmad)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/delete-post-link/#post-1587918)
 * using this at functions.php
 *     ```
       function wp_delete_post_link($link = 'Delete This', $before = '', $after = '') {
           global $post;
           if ( $post->post_type == 'page' ) {
               if ( !current_user_can( 'edit_page', $post->ID ) )
                   return;
           } else {
               if ( !current_user_can( 'edit_post', $post->ID ) )
                   return;
           }
           $message = "Are your sure you want to delete ".get_the_title($post->ID)." ?";
           $delLink = wp_nonce_url( get_bloginfo('wpurl') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID);
           $link = "<a href='" . $delLink . "' title="Delete" />".$link."";
           echo $before . $link . $after;
       }
       ```
   
 * and calling with this
 *     ```
       <?php wp_delete_post_link('Delete this', '<p>', '</p>'); ?>
       ```
   
 *  [wob](https://wordpress.org/support/users/wob/)
 * (@wob)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/delete-post-link/#post-1587919)
 * I tried this ^
 * When I press the link I get a pop up asking me to confirm, I then press “Ok”.
   Then I get a “WordPress Failure Notice”, just a blank page with the message: “
   Are you sure you want to do this?” and a link beneath that says: “Please try 
   again.” which leads back to the list post view.
 * Any ideas how to fix this?
 *  [kristarella](https://wordpress.org/support/users/kristarella/)
 * (@kristarella)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/delete-post-link/#post-1587920)
 * wob – I get the same message, don’t know how to fix it 🙁
 *  [haveboard](https://wordpress.org/support/users/haveboard/)
 * (@haveboard)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/delete-post-link/#post-1587927)
 * Same here. wp_delete_post_link() doesn’t seem to work.
 * “Are you sure you want to do this?
 * Please try again.”
 * No luck here.
 *  [sainib](https://wordpress.org/support/users/sainib/)
 * (@sainib)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/delete-post-link/#post-1587933)
 * So the problem is with nonce..WP is checking for the nonce with param name = 
   _wpnonce and pre-hash value to be in this format (without brackets of course )[‘
   trash-‘ . $post->post_type . ‘_’ . $post->ID ]
    change this line of code in function
   provided above by adanaahmad
 * `$delLink = wp_nonce_url( get_bloginfo('wpurl') . "/wp-admin/post.php?action=
   delete&post=" . $post->ID, 'delete-post_' . $post->ID);`
 * to {Changed nonce and action=trash}
 * `$delLink = wp_nonce_url( get_bloginfo('wpurl') . "/wp-admin/post.php?action=
   trash&post=" . $post->ID, 'trash-' . $post->post_type . '_' . $post->ID);`
 * So your function becomes ..
 *     ```
       function wp_delete_post_link($link = 'Delete This', $before = '', $after = '') {
           global $post;
           if ( $post->post_type == 'page' ) {
               if ( !current_user_can( 'edit_page', $post->ID ) )
                   return;
           } else {
               if ( !current_user_can( 'edit_post', $post->ID ) )
                   return;
           }
           $message = "Are your sure you want to delete ".get_the_title($post->ID)." ?";
           $delLink = wp_nonce_url( get_bloginfo('wpurl') . "/wp-admin/post.php?action=trash&post=" . $post->ID, 'trash-' . $post->post_type . '_' . $post->ID);
           $link = "<a href='" . $delLink . "' title="Delete" />".$link."";
           echo $before . $link . $after;
       }
       ```
   
 *  [sainib](https://wordpress.org/support/users/sainib/)
 * (@sainib)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/delete-post-link/#post-1587934)
 * Heres a better and ‘tested’ version of the function..
 *     ```
       function wp_delete_post_link($link = 'Delete This', $before = '', $after = '', $title="Move this item to the Trash") {
           global $post;
           if ( $post->post_type == 'page' ) {
               if ( !current_user_can( 'edit_page' ) )
                   return;
           } else {
               if ( !current_user_can( 'edit_post' ) )
                   return;
           }
           $delLink = wp_nonce_url( site_url() . "/wp-admin/post.php?action=trash&post=" . $post->ID, 'trash-' . $post->post_type . '_' . $post->ID);
           $link = '<a href="' . $delLink . '" onclick="javascript:if(!confirm(\'Are you sure you want to move this item to trash?\')) return false;" title="'.$title.'" />'.$link."</a>";
           return $before . $link . $after;
       }
       ```
   
 *  [gonzalom](https://wordpress.org/support/users/gonzalom/)
 * (@gonzalom)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/delete-post-link/#post-1587936)
 * Im having trouble with both these functions. When using an admin user, no problem.
   But when i try to delete using an author user (a post that user created), y receive
   a wordpress ‘error’ message
 *  [sainib](https://wordpress.org/support/users/sainib/)
 * (@sainib)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/delete-post-link/#post-1587937)
 * Try changing edit_page and edit_post to edit_pages and edit_posts (plural)
 * I checked wordpress documentation again just now and those are the correct capabilities.
   My GUESS is that function current_user_can returns true for admin without checking
   capability since admin has access to everything. SO give that a try.
 * If that does not work, try changing the capability to role name.. so change this
 *     ```
       if ( $post->post_type == 'page' ) {
               if ( !current_user_can( 'edit_page' ) )
                   return;
           } else {
               if ( !current_user_can( 'edit_post' ) )
                   return;
           }
       ```
   
 * TO
 *     ```
       if ( !current_user_can( 'administrator' ) && !current_user_can( 'editor' )){
                   return;
                 }
       ```
   
 * Hope this helps..
 *  [gonzalom](https://wordpress.org/support/users/gonzalom/)
 * (@gonzalom)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/delete-post-link/#post-1587938)
 * ok, i was able to solve this. It had nothing to do with the code here, but i 
   had disabled user from wp-admin and thus they couldn’t execute this link. Thanks
   anyway.
 * By the way, I wasn’t able to implement the alert messages before deleting.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Delete Post Link’ is closed to new replies.

## Tags

 * [delete](https://wordpress.org/support/topic-tag/delete/)
 * [link](https://wordpress.org/support/topic-tag/link/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [wp_nonce_url](https://wordpress.org/support/topic-tag/wp_nonce_url/)

 * 10 replies
 * 7 participants
 * Last reply from: [gonzalom](https://wordpress.org/support/users/gonzalom/)
 * Last activity: [14 years, 11 months ago](https://wordpress.org/support/topic/delete-post-link/#post-1587938)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
