1

I am getting following error message :

Parse error: syntax error, unexpected '{' in /home/otcpaneluser/public_html/mytheme/header.php on line 1

My header.php have very simple lines of code. Those are:

    <?php
    if ( !wp_is_mobile() ) { // desktop header
        echo 'I am the desktop header';
    }
    else {
        echo 'I am the mobile header';
    }
    ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<meta name="google-site-verification" content="lePo6W0p7rXhyUR2Yd21QNuJey6t9b240pkJlPbZJe4" />

  <head></head>
  <body>....</body>
</html>

Why does this happen? How can I solve this??

7
  • The header.php should be in /wp-content/themes/mytheme/header.php, are you looking in the right file? Aug 28, 2015 at 5:34
  • yeah.. header.php is in the mytheme folder Aug 28, 2015 at 5:37
  • it works fine if I do not use wp_is_mobile() Aug 28, 2015 at 5:40
  • Post your complete header file, it seems that you are adding that code right at the top, which you should not do Aug 28, 2015 at 5:50
  • ooh.. @PieterGoosen thanks for that.. exactly that's what I am doing.. check my updated code.. so how can I solve it?? Aug 28, 2015 at 6:05

1 Answer 1

1

You should not be executing anything before your DOCTYPE tag. That will all be invalid code and is also the issue leading to your error.

In general, any output like above should be done after the <head></head> tags, although some prefer to hook into the wp_head hook to execute functions like your code. I do prefer however to move such code after the closing head tag.

So your solution would be to move your conditional code to just after the closing head tag

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.