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


wp_redirect( get_permalink($pid) );

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.
  • 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, 2012 at 17:32
  • Please post this code in context.
    – s_ha_dum
    Dec 22, 2012 at 17:33
  • @s_ha_dum I've updated my question to include a pastebin
    – Anagio
    Dec 22, 2012 at 17:38
  • @Rarst I've updated the question with a pastebin of the entire code
    – Anagio
    Dec 22, 2012 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, 2012 at 17:44

2 Answers 2


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

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.

  • @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, 2012 at 17:55
  • @Anagio - added an example
    – Milo
    Dec 22, 2012 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, 2012 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, 2012 at 17:54
  • @Milo you nailed this for me. "headers already sent" was the problem for me. Thanks Sep 24, 2013 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) );
//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.

  • 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, 2012 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, 2012 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, 2012 at 18:31
  • @s_ha_dum that piece of suggestion is a piece of diamond in a nutshell. :) It explained everything. I tried a lots of ways - all the wp_loaded, template_redirect things, but could not make things work. Thanks a lot. Apr 27, 2015 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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