Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

wp_redirect($post->guid) is not working,how to solve 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);

   } 
share|improve this question

3 Answers 3

I am not sure if this will help... but I found that I had some code in a template and I was starting with get_header() in this way:

<?php
/**
 * .. Template comments
 */

 get_header();

 if(...) {
    ...
    if(...) {
      ...
      wp_redirect($url);
      exit();
    }
 }
 ?>

and was getting the same issue of header previously sent... What I did was just move get_header() to the end of the block and voila!!!

<?php
/**
 * .. Template comments
 */


 if(...) {
    ...
    if(...) {
      ...
      wp_redirect($url);
      exit();
    }
 }
 get_header();
 ?>

No plugin was disabled. and everything was ok... you may give a try if this works for you

share|improve this answer
    
This is a good way to do it, if you have access to the theme source. Redirects should run before the call to get_header. –  s_ha_dum May 1 '13 at 18:28
    
removing get_header() also worked for me! –  Magico Oct 31 at 10:01

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();
share|improve this answer
    
Beat you by 30 seconds :D –  soulseekah Mar 21 '12 at 14:45
    
this is not working, if i run the page console show 302 Found 479ms jquery...r=1.7.1 (line 4) GET localhost/wordpress/newpages-17 200 OK 1.2s loading.......... –  SANS780730 Mar 21 '12 at 14:51
2  
Thats a JS error. Nothing to do with wp_redirect. The above answer is the correct way to do it, so you must be doing something else wrong. –  Stephen Harris Mar 21 '12 at 15:00
    
sorry.it show only GET localhost/wordpress/newpages-17 200 OK 1.2s loading.......... –  SANS780730 Mar 21 '12 at 15:04
    
thank you so much. –  SANS780730 Mar 21 '12 at 16:48

Never ever use the post GUID value, it does not have to match the real URL of the post.

http://codex.wordpress.org/Changing_The_Site_URL#Important_GUID_Note

wp_redirect( get_permalink( $post_id ) );
exit(); // always exit

Also, make sure wp_redirect is not plugged by something else which prevents it from doing its job correctly. Deactivate all plugins and revert to Twenty Ten/Eleven to check.

share|improve this answer
    
+1 good call on wp_redirect being pluggable –  Stephen Harris Mar 21 '12 at 14:47
    
thaning you.... –  SANS780730 Mar 21 '12 at 16:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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