Make WordPress Core

Opened 8 years ago

Last modified 5 years ago

#35871 new defect (bug)

before_delete_post incorrectly triggered from edit.php to post.php

Reported by: jonathandejong's profile Jonathandejong Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.4.2
Component: Posts, Post Types Keywords: reporter-feedback
Focuses: administration Cc:


It seems there's a bug where before_delete_post triggers when clicking the post name (on edit.php) to go to the post.php admin edit screen. I have this code

add_action('before_delete_post', $plugin_admin, 'prevent_club_deletion', 5);
public function prevent_club_deletion() {
        global $post_type;
    if ( $post_type != 'club' )

        $redirect = ( isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : get_admin_url(1, 'edit.php?post_type=club') );
    wp_redirect( $redirect );

It randomly triggers the redirect when attempting to head into single edit screen for a post. It's a hierarchical CPT and the bug occurs for both parent and child pages. Tested with custom theme (no hooks really) and no plugins activated (except the one that creates the CPT and this hook).

Change History (5)

#1 @boonebgorges
8 years ago

  • Keywords reporter-feedback added

Hi @Jonathandejong - Welcome to WordPress Trac!

I can't think of a reason why WP itself would be triggering a post deletion when clicking on a post name from edit.php.

Your best next step is to get a backtrace. I'd suggest writing a small plugin that records the contents of $_SERVER as well as debug_backtrace() into a log file whenever wp_delete_post() is called. Then, use the site until you trigger the redirect behavior you've described.

#2 @Jonathandejong
8 years ago

Hi @boonebgorges - Thank you :)

Me neither.. I was thinking of something like revisions or maybe the heartbeat api having a role in it.
So should I use the before_delete_post hook for it or maybe pre_delete_post since it runs even earlier?

#3 @boonebgorges
8 years ago

Yes, I was thinking something like:

add_action( 'before_delete_post', function() {
    error_log( print_r( debug_backtrace(), true ) );
    error_log( print_r( $_SERVER, true ) );
}, 0 );

#4 @johnbillion
8 years ago

This might be caused by the periodic removal of auto-drafts.

#5 @Jonathandejong
8 years ago

That was my suspicion as well. I'm guessing they run on wp-cron so it would make sense that they'd run when I click the post link (interact with the website).

Note: See TracTickets for help on using tickets.