I am trying to embed a delete post link on a post layout via shortcode where the user would delete their own post. The details I am using are below. The code below seems to execute confirmation and redirect, but it doesn't delete the post? I can't figure out why. I saw previous post regarding "force delete". I'm not sure how to use that if I am not using MB Front End editor. Does MB have a better solution?
I use the following:
- MB builder for Custom Post and Fields
- Beaver Themer to display the custom post
- Fluent forms integration to populate forms
function delete_post_shortcode() {
// Check if the user is logged in
if (!is_user_logged_in()) {
return '';
}
// Get current post information
global $post;
$post_id = $post->ID;
$post_author_id = $post->post_author;
// Retrieve current user information
$current_user = wp_get_current_user();
// Check if the current user is the author of the post
if ($current_user->ID == $post_author_id) {
// Generate the delete post link with confirmation message and redirect
$delete_link = sprintf(
'<a href="%s" onclick="if(confirm(\'Are you sure you want to delete this?\')) {window.location.href = \'%s\';} return false;">Delete My Profile</a>',
get_delete_post_link($post_id, '', 'https://staging7.mysite.com/my-job-seeker-account/'),
'https://staging7.mysite.com/my-job-seeker-account/'
);
return $delete_link;
}
return '';
}
add_shortcode('delete_post', 'delete_post_shortcode');
Thank you!
Marge