wp_redirect() function is not working

wp_redirect($post->guid) is not working. How can I fix this?

This is my code:

if(isset($_REQUEST['vid']) ){

    $id=$_REQUEST['vid'];

    $post_title="sasa";

    $post_content="zxczxczxc";

    $new_post = array(
      'ID' => '',
      'post_author' => $user->ID, 
      'post_content' => $post_content,
      'post_title' => $post_title,
      'post_status' => 'publish',
      // NOW IT'S ALREADY AN ARRAY

    );

    $post_id = wp_insert_post($new_post);

    // This will redirect you to the newly created post
    $post = get_post($post_id);
    $url=$post->guid;

    wp_redirect($post->guid);

} 

8

Two things wrong here:

  1. Don’t use $post->guid as an url
  2. You must exit() after using wp_redirect() (see the Codex)

    wp_redirect() does not exit automatically and should almost always be followed by exit.

To redirect to your new post’s page:

//..... code as in question
$post_id = wp_insert_post($new_post);
$url = get_permalink( $post_id );
wp_redirect($url);
exit();

Leave a Comment