1

Whenever I use wp_redirect(plugins_url('account/login')); in a shortcode function I get the following error:

Warning: Cannot modify header information - headers already sent by ...

Is there any way to redirect from there? My guess would to echo some Javascript that does the redirect from me, but I was wondering whether there would be a server-side implementation of this.

6
  • check in your functions.php, did you have closing ?> tag at the end ? if yes remove it and try again Oct 11, 2013 at 18:08
  • @wordpresser there are no closing ?> in any of my plugin files
    – Joren
    Oct 11, 2013 at 18:11
  • You are most likely hooking it too late. How do you implement it?
    – birgire
    Oct 11, 2013 at 18:12
  • where are you trying to use it? you can't send headers after content output has started.
    – Milo
    Oct 11, 2013 at 18:12
  • I'm indeed using it from within a shortcode function. Any way to redirect from there?
    – Joren
    Oct 11, 2013 at 18:18

2 Answers 2

1

Why does it need to be a Shortcode? Looks like a XY Problem.

You're already inserting the shortcode manually in the edit screen. Use a Custom Field instead and hook earlier where you can actually do the redirect.

enter image description here

add_action( 'template_redirect', function(){
    global $post;
    $redirect = get_post_meta( $post->ID, 'redirect', true );
    if( $redirect )
    {
        wp_redirect( admin_url( 'profile.php' ) );
        exit;
    }
});
1
  • Also, redirecting to plugins_url('account/login') doesn't sound like a good idea...
    – brasofilo
    Oct 11, 2013 at 21:51
0

To redirect to another page you need to set an http header. To set an header it has to be the first thing that is output to the browser.

You are trying to set an header when something already has been output to the browser, that's why you get the warning and the redirect doesn't work.

You need to change your implementation so that the redirect is triggered before anything got output. So you should hook it to init or any other action that happens before output.

This can also happen due to another plugin which incorrectly outputs something when it shouldn't.

Your Answer

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

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