How can I hook into creating a new post and execute wp_die(), before the post is inserted into the database?

Info about wp_die from codex: Kill WordPress execution and display HTML message with error message. A call to this function complements the die() PHP function. The difference is that HTML will be displayed to the user. It is recommended to use this function only when the execution should not continue any further.

I have code:

Read More
function myHook( $post_ID, $post )
{
     wp_die('Error', 'Error',  array( 'response' => 500, 'back_link' => true ));
}
add_action( 'save_post', 'myHook', 0, 2 );

Problem is that error page appears, but post is still inserted. Shouldn’t saving be interrupted by this hook? If I misunderstood description, how can I break execution so post will not be saved?

EDIT: I just found that save_post is called after instert to database not before. Does there exist any function called before insert that i can hook into?

Thanks in advance

Related posts

Leave a Reply

1 comment

  1. Try filtering with wp_insert_post_data instead. It’s from wp-includes/post.php, line 2864.

    add_filter( 'wp_insert_post_data', 'post_publish_filter_wpse_82356' );
    function post_publish_filter_wpse_82356( $data ) {
        // view/manipulate $data
        if ('publish' == $data['post_status']) {
            $msg = '<pre>' . var_export($data, true) . '</pre>';
            wp_die($msg);
        }
        return $data;
    }
    

    publish is the post_status when you click the Publish or Update button. The four statuses I’ve found are:

    • auto-draft – self-explanatory (also, status is set to this when you click Add New)
    • draft – when you click Save Draft button
    • inherit – when you click Preview button
    • publish – when you click Publish or Update button