• Resolved baduist3

    (@baduist3)


    i’m working on a template for my new blog, and trying to get a link to ‘delete this post’ for each post on the front end (in the blog), visible only to me when i’m logged in, of course.
    something like the ‘edit this post’ link that is already showing in kubrick template.

    Can anyone give tips about this?

Viewing 7 replies - 1 through 7 (of 7 total)
  • tips?

    Look at how its done in the backend. Under manage >> posts.

    Thread Starter baduist3

    (@baduist3)

    Cool.
    i copied this code from there, but instead of deleting the post, i get a “Sorry, no posts matched your criteria”.

    <?php if ( current_user_can('delete_post',$post->ID) ) { echo "<a href='" . wp_nonce_url("post.php?action=delete&amp;post=$id", 'delete-post_' . $post->ID) . "' class='delete' onclick=\"return deleteSomething( 'post', " . $id . ", '" . js_escape(sprintf(__("You are about to delete this post '%s'.\n'OK' to delete, 'Cancel' to stop."), get_the_title())) . "' );\">" . __('Delete') . "</a>"; } ?>

    i was using it in the loop, if it makes any difference.

    well you cant copy that verbatim because what you copied isnt using the variables as they exist inside the loop.

    You need to adjust that so that when you are calling the post, you use it’s variable as its assigned there — look at how the edit link is handled, in other words.

    Thread Starter baduist3

    (@baduist3)

    ok.
    This code in the loop seems to do the trick:
    <?php if (current_user_can('edit_post', $post->ID)) echo "<a href='" . wp_nonce_url("/wp-admin/post.php?action=delete&amp;post=$id", 'delete-post_' . $post->ID) . "'>Delete post</a>" ?>

    Thanks whooami!

    youre welcome ??

    I’ve added that bit of code to single.php, but wp redirect back to the page that was just deleted, resulting in a 404. How can I avoid this and have it go back to the home page?

    Brilliant solution! Thank you!! The only thing that scares me is that there is no “are you sure?” alert before the post is actually deleted. Still, I’m a big boy. I can deal with the responsibility. ??

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘‘delete post’ on front end?’ is closed to new replies.