Permanently delete front-end published page

By: Phillip Nicholl | Asked: 09/06/2023
ForumsCategory: Code SnippetsPermanently delete front-end published page
Phillip Nicholl asked 1 year ago
Hi, I'm publishing pages on the front end. The pages contain a profile of the person currently in charge of an account. The user ID is specific to the account and not to the current occupant. The page URL slug is also generated from the user ID.  When the person in charge of the account leaves, the next occupant needs to create a new profile and the previous page for that account has to be deleted in order to use the same URL slug. Currently, the front-end delete link I'm using deletes the form entry and the page but the page is only moved to trash in the backend. I have to manually go to trash and permanently delete it, otherwise the new page published by the form has a '-2' addition to the slug and then internal links don't work. I raised this with Formidable support and was given the code snippet below which I'm implementing with the Wordpress plugin 'Snippets': My form ID is 25.

Unfortunately, deleted pages are still just being moved to Trash (Bin).

Can anyone help?

Many thanks,

Phil
add_action('frm_after_destroy_entry', 'delete_post_permanently', 20, 2);
function delete_post_permanently($entry_id, $entry){
if($entry->form_id == 25){ // Replace YOUR_FORM_ID with your actual Form ID
$post_id = get_post_meta( $entry->post_id, '_frm_wp_post_id', true );
if($post_id){
wp_delete_post( $post_id, true );
}
}
}


3 Answers
Victor Font answered 1 year ago
If Formidable created the post in the first place, this code is wrong. You're using the frm_after_destroy_entry action. This fires after the entry has been deleted. When you delete an entry that was used to create a post, the post is automatically deleted and moved to trash. This is how WordPress works. The wp_delete_post() function permanently deletes posts when you pass true as the second parameter, but not if they've already been moved to the trash. The true parameter informs the function to bypass trash. https://developer.wordpress.org/reference/functions/wp_delete_post/
Victor Font replied 1 year ago

Because I think this may be a timing issue, I suggest you use the frm_before_destroy_action entry. https://formidableforms.com/knowledgebase/frm_before_destroy_entry/

Phillip Nicholl answered 1 year ago
Hi Victor, Many thanks for your answer. My knowledge of PHP is 'slightly more than nothing' but I've looked at the frm_before_destroy_action entry and I think I can adapt it referencing the original suggested code. I'll post again tomorrow. Thank you again. Phil    
Phillip Nicholl answered 1 year ago
Hi Victor, I've tried adapting your answer but it is still just moving the deleted page to Trash. Does it need to integrate
$force_delete = true
? Here is my current snippet. My form ID is 25.
add_action('frm_before_destroy_entry', 'delete_post_permanently', 20, 2);
function delete_post_permanently($entry_id) {
  $entry = FrmEntry::getOne($entry_id);
  if ( $entry->form_id == 25 ) {
    wp_delete_post( $post_id, true);
  }
}

Many thanks, Phil

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right