InsideDarkWeb.com

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

} 

WordPress Development Asked by SANS780730 on November 21, 2021

8 Answers

8 Answers

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

Answered by rahmat hidawe on November 21, 2021

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

Answered by Farhat Aziz on November 21, 2021

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

Answered by Golap Hazi on November 21, 2021

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;

Answered by Fury on November 21, 2021

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

Answered by yogesh on November 21, 2021

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

Answered by user6181996 on November 21, 2021

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

Answered by Stephen Harris on November 21, 2021

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.

Answered by soulseekah on November 21, 2021

Add your own answers!

Related Questions

How to incluide custom field in WooCommerce email using placeholder

0  Asked on December 8, 2020 by santiago-cuartas-rmrz

 

Adding microdata using Structured Data Markup Helper HTML

1  Asked on December 6, 2020 by desislava-angelova

     

Server timeout error on every weekend

0  Asked on December 6, 2020 by dhaval-joshi

 

How do I make a shortcode to display the shop name on Dokan?

1  Asked on December 5, 2020 by sean-taylor

 

Excess dependencies

1  Asked on December 5, 2020 by lovor

   

A conditional button

1  Asked on December 3, 2020 by aashish-tripathi

       

Visual editor not saving data

0  Asked on December 2, 2020 by jasierok

     

WordPress Register Form

0  Asked on November 30, 2020

   

New post status for custom post type

3  Asked on November 30, 2020 by dclawson

   

Showing related post for custom taxonomy

1  Asked on November 28, 2020 by luca

 

How to hide “Change role to” dropdown on Users admin menu

0  Asked on November 27, 2020 by rijal-islami

     

Colliding custom permalink rules

0  Asked on November 26, 2020 by laurent

   

how to redirect the link to a category in the website

1  Asked on November 26, 2020 by jeline

 

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.