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 pretty much got my answer in my last question, but not quite. I was supposed to add one last code to my source and it would have worked but the person helping hasn't replied now and I don't know what to do :S

Form to Add Posts to Custom Post Type

So can anybody tell me where to put wp_redirect(get_permalink($pid)); exit; in my code to have it redirect to the post after the post is added to the database?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

You need to put the form processing part of the code at the top of the page before anything else and

wp_redirect(get_permalink($pid)); exit;

Right after

   

$pid = wp_insert_post($new_post); 
share|improve this answer
    
I'm still getting a headers error, this is what it says: Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\wordpress\wp-content\themes\qa\header.php:6) in C:\xampp\htdocs\wordpress\wp-includes\pluggable.php on line 897 –  Jared Mar 8 '11 at 23:23
    
whats the code you are using? –  Bainternet Mar 8 '11 at 23:30
    
also try replacing exit; with die(); –  Bainternet Mar 8 '11 at 23:33
    
I'm getting the same error, using the exact same code in my last question you told me to use and added the code just below $pid = wp_insert_post($new_post); like above and also tried switching it with die() and I get the same problem. It's pretty strange! –  Jared Mar 9 '11 at 0:06
1  
its beacuse you use "get_header" before the processing part of the code look at pastie.org/1651163 i fixed it and added a better error handeling –  Bainternet Mar 9 '11 at 12:10

You can add it on your processing section:

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {

Just below:

$pid = wp_insert_post($new_post);

Make it something like this:

$pid = wp_insert_post($new_post);
wp_redirect(get_permalink($pid)); exit;

EDIT: Oops, after reading comment on that page, seems like you put it in wrong style. See my comment on that page.

If you don't want to change current code, you can use something like this:

$pid = wp_insert_post($new_post);
?><script type="text/javascript">
<!--
window.location = "<?php echo get_permalink($pid); ?>";
//-->
</script><?php
share|improve this answer

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.