November 29, 2011

Redirection using PHP footer in Worpress

Question by The Waves

I have a question about timed redirects in PHP – specifically in wordpress.

We have setup a site using a free Woothemes placeholder theme, it is very limited. But that is OK – the site is simple.

After 20 seconds I would like the page to redirect to another URL – would it be possible to insert some code into footer.php to do this? I have found what looks like the right code:

// Redirect with a delay:
header('Refresh: 20; url=http://www.example.org/');

Can this be inserted antwhere in footer.php?

Any input is welcome.

Answer by Starx

Yes, it can be inserted, but in case some HTML is already by the server you will receive an warning such as header already sent by ..... and the redirection will not function as it should.

Instead you can perform such by clearing everything in the output buffer using ob_clear() that is to be printed and then send the redirection header.

Example:

if($casespecial==true) {
    ob_clean(); //make sure nothing is outputed to the browser
    header('Refresh: 20; url=http://www.example.org/'); //now send the header param

    //After wards, you can resume your normal code and output the template as you require
    .
    .
    .
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!