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've got a front end post editing page located at mysite/post_id/edit and am trying to redirect users that are not the author back to the post. Here's the code I'm working with:

    <?php
    global $current_user; get_currentuserinfo();
    if($post->post_author != $current_user->ID):
    wp_redirect( the_permalink() ); exit;
    ?>

The problem I've been having is that this seems to just display the post's permalink rather than redirecting.

wp_redirect says "wp_redirect will not be called if the page has started, so make sure to call it higher up." - but after having placed the code at the top of the header.php file I still received the same issue of the permalink being displayed rather than redirecting.

Where would be the appropriate place to add this code to make it redirect?

Thanks in advance!

EDIT: Tried something with milo's advice, but will follow Tommix's post before I update on Milo's

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Fully working ANSWER:

global $current_user; get_currentuserinfo();
if($post->post_author != $current_user->ID){
wp_redirect(get_post_permalink($post->ID)); 
 exit();
}
share|improve this answer
    
This current code is returning Cannot modify header information - headers already sent by –  Stephen S. May 17 '12 at 18:21
    
Strange for me it works great. I added it in single.php file. maybe your file already echos something? put the code as much hi as you can in top of php file. Of course it should also get the $post because if it not get it, it won't know if the user is author so it should go after the $post variable have data. Cause the code is good, it just bad placement :) maybe you put this code in included file? –  Tommixoft May 17 '12 at 18:26
    
You're right - I have it partially working - though oddly enough it is redirecting when ($post->post_author == $current_user->ID) rather than when ($post->post_author != $current_user->ID) –  Stephen S. May 17 '12 at 18:52
    
what happens if you try use this $post->post_author !== $current_user->ID –  Tommixoft May 17 '12 at 18:58
    
ah I see, it is unable to get the $post->author because that variable hasn't been set that early on the page –  Stephen S. May 17 '12 at 20:10
show 1 more comment

Once you're in the template it's too late, as headers have already been sent. You have to hook earlier in the request to check, like the template redirect hook:

add_action( 'template_redirect', 'wpse52455_redirect' );

function wpse52455_redirect(){
    // do your check and call wp_redirect here
}

Note that this will get called on every request, so you also need to do a check that the current page is your edit page.

EDIT - the above code should go in your theme's functions.php file.

share|improve this answer
    
would both the add_action and the function be placed in functions.php? –  Stephen S. May 17 '12 at 17:43
    
@StephenS. yes, both in functions.php. –  Milo May 17 '12 at 17:46
add comment

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.