Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using this redirect after inserting a post. It's not working, it only refreshes the page the form is on. I know the $pid is getting the post ID so what is the problem? This is the very end of my php code for handling the form submission.

$pid = wp_insert_post($new_post);

update_post_meta($pid,'domain',$domain);
update_post_meta($pid,'keywords',$keywords);

wp_redirect( get_permalink($pid) );
exit();

Here is a pastebin of the complete code

Using Better HTTP Redirects it's output is, and it links the word here to the correct newly published post.

302 Found
The document has moved here.
share|improve this question
2  
Hard to guess anything from this information, have you tried to debug it for more details? Better HTTP Redirects plugin is good tool to look into redirect issues. –  Rarst Dec 22 '12 at 17:32
    
Please post this code in context. –  s_ha_dum Dec 22 '12 at 17:33
    
@s_ha_dum I've updated my question to include a pastebin –  Anagio Dec 22 '12 at 17:38
    
@Rarst I've updated the question with a pastebin of the entire code –  Anagio Dec 22 '12 at 17:38
    
@Rarst I installed the plugin please see my updated post it displays a 302 and links to the new post but doesn't refresh there –  Anagio Dec 22 '12 at 17:44

2 Answers 2

up vote 5 down vote accepted

You can only use wp_redirect before content is sent to the browser. If you were to enable php debugging you'd see a "headers already sent" error due to get_header() on the first line.

Rather than process the form in the template, you can hook an earlier action, like wp_loaded, and save some queries to the db if you're just going to redirect away.

EDIT, example-

add_action( 'wp_loaded', 'wpa76991_process_form' );
function wpa76991_process_form(){
    if( isset( $_POST['my_form_widget'] ) ):
        // process form, and then
        wp_redirect( get_permalink( $pid ) );
        exit();
    endif;
}

Using an action, you can keep the code out of and separated from your templates. Combine this with a shortcode to output the form and wrap it all in a class to save state between processing/output, and you could do it all without touching the front end templates.

share|improve this answer
    
@Miloe yes I just saw the headers already sent message now with debugging enabled and the better http redirect plugin on. I'm not familiar with how to use the hooks, can you point me to some tutorial or show some example code please –  Anagio Dec 22 '12 at 17:55
    
@Anagio - added an example –  Milo Dec 22 '12 at 19:53
    
Thanks, so your suggesting I put the form into a short code then use do_shortcode() within the template to display the form. The hook would go into my functions.php. What does the action of the form become to fire the function/hook? –  Anagio Dec 23 '12 at 7:36
1  
you wouldn't have to use do_shortcode, my point was that you could add it via a shortcode to a post/page's content, then all your processing and rendering code is separated from the template, that way the form could work on any page you place the form's shortcode within the content of. the action can just target the current page with a #, or be blank, since you're hooking all requests to check if your form was submitted, it will work from/to any page. –  Milo Dec 23 '12 at 17:54
    
@Milo you nailed this for me. "headers already sent" was the problem for me. Thanks –  henrywright Sep 24 '13 at 15:38

Moving get_header(); to the bottom of that code should fix the problem. Your code will execute before any headers are sent and the redirect will work.

// ...
wp_redirect( get_permalink($pid) );
exit();
//insert taxonomies
} 
get_header(); ?>

I assume there is more code on the page below what you posted? If not I don't see the need for get_header() at all.

The only benefit I can see to using a hook as Milo suggests is that you might be able to avoid some overhead if you pick an early enough hook. You could shave a fraction of a second off of processing.

share|improve this answer
    
Yes there's some HTML, and some more wp functions get_sidebars(), and get_footer() etc. I'm not at all familiar with using hooks but would really like to see an example. I'm already googling and see people talking about add_action('wp_loaded', 'your_function') but really not sure how to use it. Any examples is appreciated thanks –  Anagio Dec 22 '12 at 18:22
    
I'll wait awhile and see if @Milo posts an example using a hook, since that is his answer. If not, I'll edit my answer. –  s_ha_dum Dec 22 '12 at 18:25
    
Thanks moving the get_header() below the form handling code and redirect worked. I would like to see how to use the hook though. –  Anagio Dec 22 '12 at 18:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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