WordPress Development Asked by SANS780730 on November 21, 2021
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);
}
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:
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.
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:
$post->guid
as an urlexit()
after using wp_redirect()
(see the Codex)
wp_redirect()
does not exit automatically and should almost always be followed by exit.
//..... 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
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP