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);
}
Two things wrong here:
$post->guid
as an urlexit()
after usingwp_redirect()
(see the Codex)To redirect to your new post’s page:
I have a simple solution, please read:
If you are using
wp_redirect($url)
in theme files, and it is not working addob_clean() ob_start()
in your function file on top.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: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:
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!!!
No plugin was disabled. and everything was ok… you may give a try if this works for you
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
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.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.
I had same problems here,
I just try any method like
finally i got hook that can work with perfectly redirect,
below my full code in functions.php
just use hook add_action( ‘template_redirect’, ‘my_function_name’);
Nb: i use child-theme
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 useob_start();
the redirection won’t work.and then simply try this code:
Just noted that W3 total cache plugin must be active for this to work. It disabled mine for some reason.