2

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:

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

1 Answer 1

2

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
6
  • According to codex it works only for updates. It's not called on inserts. Commented Jan 20, 2013 at 8:36
  • @user1666774: See my updated Answer.
    – akTed
    Commented Jan 20, 2013 at 8:43
  • Problem is that in this filter i don't have access to data from add_meta_box Commented Jan 20, 2013 at 9:13
  • See my updated (again - I've finally tested enough, hopefully no more edits :) ) answer for the statuses I've found and updated example code. Regarding add_meta_box data, that'll have to be another Question.
    – akTed
    Commented Jan 20, 2013 at 9:24
  • It's possible you can find the add_meta_box data in one of WP's global variables or pull it from a function call.
    – akTed
    Commented Jan 20, 2013 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.