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 |1 Answer
Reset to default 1Following 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
$post
and$id
are not defined in your code. And you should better off useget_delete_post_link()
. – Sally CJ Commented May 15, 2019 at 2:02