Delete Post Link

August 25th, 2008

In most Wordpress themes, if you are logged in, there is an “Edit Post” link. That way you can go directly into edit the current post.

I have created a function called “Delete Post Link”. If you are logged in you will see a link which allows you to delete the current page or post, even without visiting admin.

Warning! You should be very careful using this function!

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;
	}
	$link = "<a href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&amp;post=" . $post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>";
	echo $before . $link . $after;
}

I recommend you to put the function into your functions.php file in your theme folder. When done just put the function into your theme like this:

wp_delete_post_link('Delete This', '<p>', '</p>')

To get all new functions/template tags, install my plugin WP Extra Template Tags.

  1. 2010-01-26 - Jordan

    Thanks. Very helpful.

  2. 2009-12-10 - peter

    I would like to get a confirmation, now it deletes the post immediately, that sux.

    Can you implement this?

  3. 2009-11-17 - baseball2525

    This is very useful and hard to find lol! You should make it a plugin so it can get some recognition :D Thanks!

  4. 2009-11-10 - manaa

    Is it possible to get a confirmation before deleting?

  5. 2009-06-03 - Admi

    thanks man. I’ll translate it on Russian

  1. » Delete Post Link