Shortcode to delete post from front end

I'm trying to create a short code to delete post that user publish from the front end of my site.

I've tried the short-code below but I keep getting the error "The link you followed has expired." Kindly see image screenshot below.

  //Shortcode to delete post
    function delete_my_posts() {
        ob_start();

            $url = get_bloginfo('url');
     if (current_user_can('edit_post', $post-ID)){
       echo 'a class="delete-post" rel=”nofollow” href="';
       echo wp_nonce_url("$url/wp-admin/post.php?action=trashpost=$id", 'delete-post_' . $post-ID);
       echo '"Delete post/a';
     }

       return ob_get_clean();    
    }
    add_shortcode( 'delete_me', 'delete_my_posts' );

Screenshot: https://imgur.com/a/PMB1uU4

Can someone kindly advise on this error?

Thanks

Topic trash shortcode Wordpress

Category Web


Following is the reformatted version of your code. get_delete_post_link() is used for fetching delete post URL so that we dont have to worry about nonce stuff. global $post is kept to avoid PHP notice which is currently there in your code. Please check it.

function wpso_delete_my_posts() {
    global $post;
    ob_start();
    if ( current_user_can('delete_posts', $post->ID ) ) {
        echo '<a class="delete-post" rel="nofollow" href="' . esc_url( get_delete_post_link( $post->ID ) ) . '">Delete Post</a>';
    }
    return ob_get_clean();
}
add_shortcode( 'delete_me', 'wpso_delete_my_posts' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.