Script at top of header.php breaks website (no error) (5 posts)

  1. pealo86
    Posted 4 years ago #

    I'm using the following code to forward my user should they try to visit a single custom post page. This is how header.php starts:

    	// forward necessary single custom posts
    	if(is_singular('production')) {
    		header('Location: ' . get_permalink(11));
    	} elseif(is_singular('credit')) {
    		header('Location: ' . get_permalink(13));
    ...rest of code goes here

    However it is just giving me a white screen of death, and if I set 'WP_DEBUG' to 'true' I don't get any error message.

    Can anyone see an issue here? I've had this same problem on several other websites however on some websites it works completely fine (all on the same hosting account too!)

  2. WPyogi
    Forum Moderator
    Posted 4 years ago #

    You cannot have any code above the doctype.

  3. pealo86
    Posted 4 years ago #

    Wow really? :O

    I'm sure I've used this code fine on other websites. So is there no way to perform a redirect using PHP on a WordPress site?

  4. Chip Bennett
    Theme Review Admin
    Posted 4 years ago #

    Try removing the exit; at the end. Basically, what this code is saying is:

    1. If this is a single "production" post-type, redirect to post 11
    2. Else, if this is a single "credit" post-type, redirect to post 13
    3. Otherwise, stop processing

    I don't see how you'd ever get anything to parse, using this code.

  5. pealo86
    Posted 4 years ago #

    Ahh yes good point! I was under the impression that you always had to use an exit statement after changing the header location?

    I'll try moving it to within the if statements


Topic Closed

This topic has been closed to new replies.

About this Topic