WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. pealo86
    Member
    Posted 2 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:

    <?php
    	// forward necessary single custom posts
    	if(is_singular('production')) {
    		header('Location: ' . get_permalink(11));
    	} elseif(is_singular('credit')) {
    		header('Location: ' . get_permalink(13));
    	}
    	exit;
    ?>
    <!DOCTYPE HTML>
    
    ...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
    Volunteer Moderator
    Posted 2 years ago #

    You cannot have any code above the doctype.

  3. pealo86
    Member
    Posted 2 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 2 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
    Member
    Posted 2 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

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic