3

I am making front-end post submit. and I almost completed. I want to redirect to specific page after submitting post in front-end successfully.

I am using this function.

wp_insert_post( array $postarr, bool $wp_error = false )

and this returns :

The post ID on success. The value 0 or WP_Error on failure.

$post_id = wp_insert_post( $post_data );
if( !$post_id || ?? ){ 
    wp_redirect( site_url('/compledted/') );
}

This is my code to make.I just want to redirect when submitting post is successfully finished. but I exactly don't know how to set if( ) conditions. can you help me? Thank you.

2
  • 1
    the value 0 or WP_Error depends of the argument $wp_error. then you test if (0 === $post_id) {// error} or if (is_wp_error($post_id)) {// error}
    – mmm
    Commented Apr 8, 2018 at 7:59
  • Thank you for your reply. I am trying to use the code you recommend, but how can I test an error? how can I set errors occur?
    – JJang
    Commented Apr 8, 2018 at 8:31

1 Answer 1

1

you can test an error like that

$post_id = wp_insert_post($post_data, TRUE);

if (is_wp_error($post_id)) { 
    // there is an error

}

wp_redirect(site_url('/compledted/'));
exit();
1
  • I mean, which situation makes errors. I can check if the wp_insert_post() succeed or not. But I don't know this works well when errors occur. So I want to check, when errors occur.
    – JJang
    Commented Apr 8, 2018 at 12:05

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.