0

I created a little private plugin for me, which contains a form, that saves data to a database.

Every user should only have one dataset. If he already has one, he should only be able to edit his current one (similar to profile settings).

Thats no problem, works great. But I have one issue: I want to redirect a user to his edit page, if he views the main plugin page (like site.com/submit/ -> site.com/submit/?id=das12d&edit=1)

So building this url is also no problem. But here is the real issue: I tried using this:

wp-redirect( $url );

And

header('Location: '.$url);

But both causing a Cannot modify header information - headers already sent by... error

How can I solve it?

edit: Some sample code:

function login_data() {
    global $current_user;

    if (is_user_logged_in()) {
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            show_submission($id);
        } else {
            } elseif(has_submission()) {
                $url = 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"].'?id='.$get_submission_id();
                header('Location: '.$url);
            } else {
                show_form();
            }
        }
    }
}
7
  • You cannot redirect, or set any headers after sending output. Your plugin is sending output to the browser sometime before you call wp_redirect. Commented Jun 20, 2015 at 18:11
  • Please post more code. To answer this we need more context. Why don't you want to use a filter or action hook?
    – s_ha_dum
    Commented Jun 20, 2015 at 19:03
  • Hi I added the sample code. To be real: I dont know how to handle such thing with a filter/action.
    – Zoker
    Commented Jun 20, 2015 at 19:25
  • I'm guessing you're sending output before calling login_data. Look at any calls to login_form and verify that no output has been sent yet. Commented Jun 20, 2015 at 21:37
  • How are you calling the login_data() function? Need more code Commented Jun 21, 2015 at 0:37

1 Answer 1

2

Not too sure what is really going on there, but one thing for sure your if statement is flawed, you should remove unneeded ELSE, should be like this:

if (is_user_logged_in()) {
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            show_submission($id);
        } elseif(has_submission())  
             {
                $url = 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"].'?id='.$get_submission_id();
                header('Location: '.$url);
            } else {
                show_form();
            }
        }

OR like this:

if (is_user_logged_in()) {
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            show_submission($id);
        } else {
             if(has_submission()) {
                $url = 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"].'?id='.$get_submission_id();
                header('Location: '.$url);
            } else {
                show_form();
            }
        }
    }

Maybe this will fix the error

3
  • Thanks for the answer. I agree, that unneeded ELSE statements should be removed, but why should it solve the problem?
    – Zoker
    Commented Jun 22, 2015 at 14:42
  • If there was a flaw, and show_submission was creating some output then it would cause that error Commented Jun 22, 2015 at 14:47
  • If this doesn't solve the problem, then you have something else in another function that is creating output before this function. I would use JS redirect if nothing worked. Commented Jun 22, 2015 at 14:51

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.