11

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);

} 
0

8 Answers 8

28

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();
6
  • Beat you by 30 seconds :D
    – soulseekah
    Mar 21, 2012 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, 2012 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. Mar 21, 2012 at 15:00
  • sorry.it show only GET localhost/wordpress/newpages-17 200 OK 1.2s loading..........
    – SANS780730
    Mar 21, 2012 at 15:04
  • @StephenHarris would you mind looking over my redirect question at wordpress.stackexchange.com/q/76991/10413 I've also tried your code from this answer using $pid but still can't get it to work. Thanks
    – Anagio
    Dec 22, 2012 at 17:33
11

I have a simple solution, please read:

  1. If you are using wp_redirect($url) in theme files, and it is not working add ob_clean() ob_start() in your function file on top.

  2. If using in plugin add ob_clean() ob_start() in the main plugin file on top.

And make sure you have added exit() function after wp_redirect($url) Like this:

$url = 'http://example.com';
wp_redirect($url);
exit();
4
  • This solves the issue with Mozilla Firefox returning 200 instead of 302 for the redirection to take place. Chrome redirects while Firefox doesn't. This fix helps. Thank you! Sep 12, 2018 at 13:36
  • This is a more detailed answer if you are creating a plugin or designing template. worked for me. Jul 10, 2019 at 8:43
  • I've been struggling to make this work in my custom theme... Works like a charm... Oct 8, 2019 at 10:24
  • Note, that as mentioned in another answer, that on templates you need to do the login check and redirect before the 'get_header()' function, or put your login check function n the add_filter('init','mylogincheck') in your template. Otherwise, there will be a 'headers already sent' error that might not show up in your error log (depending on error settings). Mar 27 at 20:46
9

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

3
  • 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, 2013 at 18:28
  • removing get_header() also worked for me!
    – Magico
    Oct 31, 2014 at 10:01
  • I'd bet this is the most common cause for most people struggling with wp_redirect not working
    – joehanna
    Feb 25, 2019 at 5:53
5

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.

2
  • +1 good call on wp_redirect being pluggable Mar 21, 2012 at 14:47
  • thaning you....
    – SANS780730
    Mar 21, 2012 at 16:49
1

header already sent is main reason. As header already sent, so its unable to resend it and fails to redirect. Use before header like in init hook.

add_action('init', 'your_app_function');
0

Make sure you don't have: get_header(); or any wordpress function that potentially creates contents like header and footer in your template. Otherwise the redirection won't work.

Some developers try to clear the page by using ob_start(); but if you have content in your page even if you use ob_start(); the redirection won't work.

and then simply try this code:

wp_redirect(get_permalink($post->ID));
exit;
0
if( is_page( ['wfp-dashboard', 'wfp-checkout'] ) ){
   if(!is_user_logged_in()){
      @ob_flush();
      @ob_end_flush();
      @ob_end_clean();
      wp_redirect( wp_login_url() );
      exit();
   }
}
1
  • Can you include some explanation of how this fixes the issue? I'm not sure why there are output buffers in the code, and they have the @ operator, @ doesn't prevent errors from happening, it just hides them from the error log
    – Tom J Nowell
    Jul 22, 2020 at 8:53
0

I had same problems here, I just try any method like

add_action( 'init', 'my_function_name'); or add_action( 'wp_head', 'my_function_name'); but it's not working

finally i got hook that can work with perfectly redirect, below my full code in functions.php

function redirect_homepage(){
    ob_clean();
    ob_start();
    $args = array(
        'public'   => true,
        '_builtin' => false,
     );
    
     $output = 'names'; // names or objects, note names is the default
     $operator = 'and'; // 'and' or 'or'
    
     $post_types = get_post_types( $args, $output, $operator ); 
        if(is_singular($post_types)){

            $url = get_bloginfo('url');
            wp_redirect($url, '301');
            exit();
    }
}
add_action( 'template_redirect', 'redirect_homepage');

just use hook add_action( 'template_redirect', 'my_function_name');

Nb: i use child-theme

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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