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

} 

Related posts

Leave a Reply

9 comments

  1. 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();
    
  2. 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();
    
  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

  4. 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');
    
  5. 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

  6. 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;
    
  7. 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();
       }
    }