trash - 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 th

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=trash&post=$id", 'delete-post_' . $post->ID);
       echo '">Delete post</a>';
     }

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

Screenshot: .jpg

Can someone kindly advise on this error?

Thanks

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=trash&post=$id", 'delete-post_' . $post->ID);
       echo '">Delete post</a>';
     }

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

Screenshot: https://i.sstatic/U3iuJ.jpg

Can someone kindly advise on this error?

Thanks

Share Improve this question asked May 14, 2019 at 21:12 Kendell DanielKendell Daniel 131 silver badge5 bronze badges 1
  • $post and $id are not defined in your code. And you should better off use get_delete_post_link(). – Sally CJ Commented May 15, 2019 at 2:02
Add a comment  | 

1 Answer 1

Reset to default 1

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' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745493317a4630083.html

相关推荐

  • trash - 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 th

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信