Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Redirect page just once if query string param is present

So I have the following method from the Instagram Basic Display API:

public function get_authorization_window(): string
    {
        $auth_url = $this->instagram->get_api_oauth_url() . '?' . http_build_query([
            'client_id' => $this->instagram->get_client_id(),
            'redirect_uri' => admin_url('options-general.php?page=instagram_auth'),
            'scope' => 'user_profile,user_media',
            'response_type' => 'code',
            'state' => admin_url('options-general.php?page=instagram_auth'),
        ]);

        return '<a class="button button-primary" href="' . $auth_url . '">Please click here to authorize Instagram for this site</a></p>';
    }

Now, when it builds the URL, it returns a code=dsgzdfsgzsfsbfb parameter.

Here is the URL that I get in return: https://test.com.local/wp-admin/options-general.php?code=fsdgsfgfbxf.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is where I would like to be redirected: https://test.com.local/wp-admin/options-general.php?page=instagram_auth&code=fsdgsfgfbxf.

The following param need to get added: page=instagram_auth&.

I tried using this piece of code, but I am stuck in a redirect loop:

if (!empty($_GET['code'])) {
     $this->instagram->set_code($_GET['code']);
     header('Location:' . $config['redirect_uri'] . '&code=' . $_GET['code']);
     die();
}

Does anyone know how I can call a redirect but redirect just once and maybe build a link based on the code param?

>Solution :

Here is the URL that I get in return:

https://test.com.local/wp-admin/options-general.php?code=fsdgsfgfbxf.

Here is where I would like to be redirected:

https://test.com.local/wp-admin/options-general.php?page=instagram_auth&code=fsdgsfgfbxf.

The order doesn’t really matter. If you only have the first string, the following URL should be just as sufficient:

https://test.com.local/wp-admin/options-general.php?code=fsdgsfgfbxf&page=instagram_auth

Notice, I just add the param on at the end. This only requires that you append the &page=instagram_auth param to the string you already have.

The page should work the same, no matter what order the GET params are in.

It’s difficult to say what exactly is going on, but maybe try?

if (!empty($_GET['code']) && $_GET['done'] != "1")) {
     $this->instagram->set_code($_GET['code']);
     header('Location:' . $config['redirect_uri'] . '&done=1&code=' . $_GET['code']);
     die();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading