7

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

3 Answers 3

15

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.

2
  • would both the add_action and the function be placed in functions.php?
    – Stephen S.
    Commented May 17, 2012 at 17:43
  • @StephenS. yes, both in functions.php.
    – Milo
    Commented May 17, 2012 at 17:46
-2

you might need to add some 'priority' to the action call. Like:

add_action('template_redirect', 'user_logged_in', 1, 1);
-3

Fully working ANSWER:

global $current_user; get_currentuserinfo();
if($post->post_author != $current_user->ID){
wp_redirect(get_post_permalink($post->ID)); 
 exit();
}
6
  • This current code is returning Cannot modify header information - headers already sent by
    – Stephen S.
    Commented May 17, 2012 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
    Commented May 17, 2012 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.
    Commented May 17, 2012 at 18:52
  • what happens if you try use this $post->post_author !== $current_user->ID
    – Tommixoft
    Commented May 17, 2012 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.
    Commented May 17, 2012 at 20:10

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.