Support Forum
Support › MB Custom Post Type › Create a Delete Post link on Custom Post
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:
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
More ifon of what I tried: Deactivated plugins (all that were not related to the page being rendered), checked debug (no errors), checked capabilities to be sure the user can delete. Job-seeker-profile is set to custom capabilties in Metabox.
Hello,
You can try to change the Capability Type of the CPT to Post or Page and recheck this issue. If the user can delete the post, there should be an issue with the capability of the user.
Or you can follow this article to delete a post https://www.isitwp.com/delete-post-from-front-end/