Direct link that will delete a WordPress post (PHP)

This is a bit of very badly written PHP code that will add a link under each post.
When clicking on “Delete Post” the post will instantly get deleted.

Only when you’re logged in as a administrator you’ll see the link.
Add this to your themes index.php file somewhere inside the loop.

if (current_user_can('administrator')) {
	
    global $post;
	$thepostid = $post->ID;
	echo "<center>The Post ID: " . $thepostid . " ";
	$thepostdeletelink = get_delete_post_link ( $id = $thepostid, $deprecated = '', $force_delete = false );

	echo "<a href=\"$thepostdeletelink\" target=\"iframedelete\">Delete</a></center>";
	echo '<iframe src="#" name="iframedelete"  width="2" height="2" scrolling="no"></iframe><br>';
}

Use it carefully since when clicked, it deletes the post instantly. That’s all.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.