Title: before_delete_post &#8211; problems
Last modified: August 30, 2016

---

# before_delete_post – problems

 *  [_Lui_](https://wordpress.org/support/users/_lui_/)
 * (@_lui_)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/before_delete_post-problems/)
 * Hey guys,
 * i made myself a plugin. It inserts/edits/deletes WP-Posts into another database.
   But i want to have an error message if the DB-Connection fails. If i dont “stop”
   inserting/editing/deleting if the connection is not established, it is going 
   to get really annyoing.
 * This is my code (just getting started coding with WP 🙂 )
 * ——————————————–
    function project_insertDB(){ //insert post }
 * function project_deleteDB(){
    //delete post }
 * function project_editDB()<7
    //edit post }
 * add_action(‘publish_project’, ‘project_insertDB’);
    add_action(‘before_delete_post’,‘
   project_deleteDB’); add_action(‘edit_post’, ‘project_editDB’); ——————————————————–
 * I thought about a simple
 * if(!$connection){
    exit(“No database connection”) }
 * inside each function with different messages. The problem is, it fires the “project_insertDB”
   even if i am just editing and it doesnt prevent WP from f.e. deleting an already
   existing post if you want to put it into the trash. It shows the error message,
   but didnt stop putting it into the trash. Is there a way to, lets say, stop WP
   from doing someting? And to net let fire “project_insertDB” if i am just editing
   a post?
 * I am not a native speaker, so i hope that makes sense ;P
 * Thanks in advance,
    Lui

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Bhavesh Patel](https://wordpress.org/support/users/go4glory/)
 * (@go4glory)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/before_delete_post-problems/#post-6494651)
 * Please check [trash_post](https://codex.wordpress.org/Plugin_API/Action_Reference/trash_post)
 * Run actions on post, page, or custom post type is about to be trashed. The post
   ID is passed to the action hook.
 *     ```
       add_action( 'wp_trash_post', 'function_to_run_on_post_trash' );
   
       function function_to_run_on_post_trash( $post_id ){
       $one = 1;
       $two = 2;
       $result = $one + $two;
       }
       ```
   
 * To add hook before inserting post to DB, you can use [pre_post_update](http://wordpress.stackexchange.com/questions/11993/wordpress-hook-before-inserting-post-into-database)
 *  Thread Starter [_Lui_](https://wordpress.org/support/users/_lui_/)
 * (@_lui_)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/before_delete_post-problems/#post-6494767)
 * Thanks, that pre_post_update really helped a lot!! 🙂
 * So the last problem is f.e. if you created a post and it got into the database,
   nothing wrong with that. But if you want to delete the post without a connection
   the error message pops up, the post stays in the DB, but it gets deleted on the
   blog. So its not possible (if the connection resumes) to delete it from the DB.
 * So is there a way to tell WP “stop what you are doing”, “dont delete the post”
   or “make a rollback”. I hope you get what i mean.
 * Thanks for the help!! 🙂
 *  [Bhavesh Patel](https://wordpress.org/support/users/go4glory/)
 * (@go4glory)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/before_delete_post-problems/#post-6494774)
 * Below are the ways you can tell wordpress like “stop what you are doing”
 * **1. To control Trash action (on All Page/Published post page)**
 *     ```
       add_action( 'wp_trash_post', 'function_to_run_on_post_trash' );
   
       function function_to_run_on_post_trash( $post_id ){
   
          //Check Your connection if found anything wrong then execute below code
          //f.e
          if(connection_not_found){
             echo '<div class="error">
                       <p>Sorry you can't delete blog rightnow</p>
                   </div>';
             exit; /* MUST */
         }
       }
       ```
   
 * **2. To control Permanently Delete action (on Trash Page)**
 *     ```
       add_action( 'before_delete_post', 'func_to_run_on_permanently_delete' );
   
       function func_to_run_on_permanently_delete( $post_id ){
   
          //Check Your connection
          //if found anything wrong then execute below code
          //f.e
          if(connection_not_found){ /* Or Anything similar */
             echo '<div class="error">
                       <p>Sorry you can't delete blog Permanently rightnow</p>
                   </div>';
             exit; /* MUST */
         }
       }
       ```
   
 * Make Your error message stylish and will work like charm.
 *  Thread Starter [_Lui_](https://wordpress.org/support/users/_lui_/)
 * (@_lui_)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/before_delete_post-problems/#post-6494794)
 * Works perfectly now, thank you very much 😉 !!!
    Posts wont get deleted in WP
   and if the connection resumes it works again 🙂
 * Its just that after i click “publish” (or delete/edit etc) it opens a new window
   displaying the error message (…/wordpress/wp-admin/post.php)
    Is there a way 
   to not change the page and just diaply it in the “edit-window”? I tried to display
   a error-message with ‘toastr’ (ajax/json), but it doesnt seem to work, it just
   displays a blank page with the code.
 * Thank you for your help so far!
 *  [Bhavesh Patel](https://wordpress.org/support/users/go4glory/)
 * (@go4glory)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/before_delete_post-problems/#post-6494796)
 * To display error message on the same page follow below tactic.
 *     ```
       add_action( 'before_delete_post', 'function_to_run_on_post_trash' );
   
       function function_to_run_on_post_trash( $post_id ){
           if(connection_not_found){ /* Or Anything similar */
       	wp_redirect( admin_url('edit.php?post_status=trash&post_type=post&my_unique_flag=error'));
       	exit;
           }
       }
       /*Handle GET parameter*/
       if(isset($_GET['my_unique_flag'])){ /*MUST be unique*/
       	if($_GET['my_unique_flag'] == 'error'){
       		echo '<div class="error">
       	             <p>Dear user, You are not allowed to do this...!! :)</p>
       	         </div>';
       	}
       }
       ```
   
 * Put this below code in both the function
 *     ```
       if(connection_not_found){ /* Or Anything similar */
       	wp_redirect( admin_url('edit.php?post_status=trash&post_type=post&my_unique_flag=error'));
       	exit;
       }
       ```
   
 * Also change **wp_redirect** url as per your edit,delete action.
 * If above code does not work as per your requirement than do let me know.
 * Have a good day… 🙂
 *  Thread Starter [_Lui_](https://wordpress.org/support/users/_lui_/)
 * (@_lui_)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/before_delete_post-problems/#post-6494803)
 * Redirect works like a charm, but it just doesnt display the errormessage on the
   page. Do i have to enable error-reporting or anything in wordpress?
 * Thanks again 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘before_delete_post – problems’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 6 replies
 * 2 participants
 * Last reply from: [_Lui_](https://wordpress.org/support/users/_lui_/)
 * Last activity: [10 years, 9 months ago](https://wordpress.org/support/topic/before_delete_post-problems/#post-6494803)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
