Fix php code so that doctype is on the first line of code

I added a menu to my wordpress template that would appear at the top of my web pages, but it seems to have forced my "Doctype" to appear after the menu, which has caused a rendering problem in some browsers, particularly IE.

Here is my php code, I am not by any means proficient in php. I know enough to copy and paste for the most part.  Any help is greatly appreciated the following is the code in my header.php:

<?php
/**
 * The Header for Customizr.
 *
 * Displays all of the <head> section and everything up till <div id="main-wrapper">
 *
 * @package Customizr
 * @since Customizr 1.0
 */
?>
<!DOCTYPE html>
<!--[if IE 7]>
<html class="ie ie7" <?php language_attributes(); ?>>
<![endif]-->
<!--[if IE 8]>
<html class="ie ie8" <?php language_attributes(); ?>>
<![endif]-->
<!--[if !(IE 7) | !(IE 8)  ]><!-->
<html <?php language_attributes(); ?>>                          
<!--<![endif]-->
<?php tc__f('rec' , __FILE__ , __FUNCTION__ ); ?>

      <?php do_action( '__before_body' ); ?>

      <body <?php body_class(); ?> itemscope itemtype="http://schema.org/WebPage">
           
            <?php do_action( '__before_header' ); ?>

               <header class="tc-header clearfix row-fluid" role="banner">
                 
                  <?php do_action( '__header' ); ?>

            </header>

            <?php
             //This hook is filtered with the slider
            do_action ( '__after_header' )
            ?>
deitercat2Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Chris StanyonConnect With a Mentor Commented:
Right. I haven't played with child themes much, but the one thing that does look odd is this bit:

if(! is_admin()){
   wp_nav_menu( array( 'theme_location' => 'topnav', 'container_class' => 'topnav-menu' ) );
}

Open in new window

Normally, that goes in your HTML where you want to output the menu, not in your functions file, so in your case try moving it into your header.php file:

<body <?php body_class(); ?> itemscope itemtype="http://schema.org/WebPage">
           
   <?php
   if(! is_admin()){
      wp_nav_menu( array( 'theme_location' => 'topnav', 'container_class' => 'topnav-menu' )    );
   }
   ?>

   <?php do_action( '__before_header' ); ?>

Open in new window

0
 
sammySeltzerCommented:
Do you really need a doctype in php?
0
 
Chris StanyonCommented:
Do you really need a doctype in php?
The PHP generates the HTML, which does need a DOCTYPE
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Chris StanyonCommented:
@deitercat2 - can't really see anything in your code that would cause the problem - can you post a link to your site (won't help with the PHP, but seeing the structure might give some clues)
0
 
deitercat2Author Commented:
Http://www.wifss.ucdavis.edu

When you view the source, you see the doctype appears after the menu.
0
 
Chris StanyonCommented:
Hmmm. Strange - I don't think your header.php file is causing the problem as everything looks OK.

The do_action() is telling wordpress to fire various functions that are 'hooked' in, and one of these hooks is firing a function that's not working properly. Try commenting them out one-by-one:

<?php // do_action( '__before_header' ); ?>

Your site won't work properly, but you will be able to identify which hook is causing the menu to load in the wrong place. Removing one of the hooks will remove the code from the menu - it's probably going to be do_action( '__header' );

I have no idea what the tc__f() function call is!
0
 
deitercat2Author Commented:
I commented out the code line by line and the structure remained the same with the menu loading first on the page and in the source each time.

It may be helpful to show you my function.php.  This is part of a child function.php:


<?php function my_analytics() {

/*Google Analytics Script (Child Theme Specific)*/

?>



 
            <script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-45173610-1', 'ucdavis.edu');
  ga('send', 'pageview');

</script> 
    <?php
}


/*Custom Menu for Top Navigation (Child Theme Specific) */


register_nav_menu( 'topnav', __( 'Top Nav Menu', 'customizr' ) );

if(! is_admin()){

wp_nav_menu( array( 'theme_location' => 'topnav', 'container_class' => 'topnav-menu' ) );
}



/* Navigation hover fix* (child Theme Specific */



add_filter('tc_menu_display', 'acub_menu_display');
function acub_menu_display($output) {
      echo preg_replace('| class="dropdown-toggle" data-toggle="dropdown" data-target="#"(.+?)<b |', ' class="a-stripped" $1</a><a href="#" class="dropdown-toggle a-caret" data-toggle="dropdown" data-target="#"><b ', $output, -1);
      }
0
 
deitercat2Author Commented:
Well  Ace, that did the trick...sort of.  On my development site the doctype is loading before the menu, however when I copied the code to production nothing changed almost nothing that is.

On development  and production the change  has caused my Social icons which appear on a navbar in the middle and in the footer to disappear.

Here is a link to the development  http://128.120.126.64/

Here is the link to production http://128.120.126.65/
0
 
Chris StanyonCommented:
OK. We're getting somewhere :)

On your development server, the code is now below the DOCTYPE, but it's still above the HEAD, so it's still not right.

On the production server, nothing's changed - your code is still showing above DOCTYPE. Make sure you've uploaded the header.php and the functions.php to your shild theme folder.

Post your header.php file again, so we can see where you've put the wp_nav_menu() function call.
0
 
deitercat2Author Commented:
I placed the wp_nav_menu fuction() call above the  __before body because placing  after the body class() took out the image slider.

<?php
/**
 * The Header for Customizr.
 *
 * Displays all of the <head> section and everything up till <div id="main-wrapper">
 *
 * @package Customizr
 * @since Customizr 1.0
 */
?>
<!DOCTYPE html>
<!--[if IE 7]>
<html class="ie ie7" <?php language_attributes(); ?>>
<![endif]-->
<!--[if IE 8]>
<html class="ie ie8" <?php language_attributes(); ?>>
<![endif]-->
<!--[if !(IE 7) | !(IE 8)  ]><!-->
<html <?php language_attributes(); ?>>                          
<!--<![endif]-->
<?php tc__f('rec' , __FILE__ , __FUNCTION__ ); ?>

 <?php
   if(! is_admin()){
      wp_nav_menu( array( 'theme_location' => 'topnav', 'container_class' => 'topnav-menu' )    );
   }
   ?>

      <?php do_action( '__before_body' ); ?>

      <body <?php body_class(); ?> itemscope itemtype="http://schema.org/WebPage">
           
            <?php do_action( '__before_header' ); ?>

               <header class="tc-header clearfix row-fluid" role="banner">
                 
                  <?php do_action( '__header' ); ?>

            </header>

            <?php
             //This hook is filtered with the slider
            do_action ( '__after_header' )
            ?>
0
 
Chris StanyonCommented:
Because the wp_nav_menu() call produces HTML, it has to go after the <body> tag, otherwise your HTML will be messed up. All HTML code has to go between the <body> tags.

Put it where I suggested and upload it and I'll see if I can see why the slider messes up.

Heading out now for a while, but will look at it later.
0
 
deitercat2Author Commented:
Here is the header.php code as it stands now.  Slider seems not to be affected  by the change.


<?php
/**
 * The Header for Customizr.
 *
 * Displays all of the <head> section and everything up till <div id="main-wrapper">
 *
 * @package Customizr
 * @since Customizr 1.0
 */
?>
<!DOCTYPE html>
<!--[if IE 7]>
<html class="ie ie7" <?php language_attributes(); ?>>
<![endif]-->
<!--[if IE 8]>
<html class="ie ie8" <?php language_attributes(); ?>>
<![endif]-->
<!--[if !(IE 7) | !(IE 8)  ]><!-->
<html <?php language_attributes(); ?>>                          
<!--<![endif]-->
<?php tc__f('rec' , __FILE__ , __FUNCTION__ ); ?>

 <?php
   if(! is_admin()){
      wp_nav_menu( array( 'theme_location' => 'topnav', 'container_class' => 'topnav-menu' )    );
   }
   ?>

      <?php do_action( '__before_body' ); ?>

      <body <?php body_class(); ?> itemscope itemtype="http://schema.org/WebPage">
     
      <?php
   if(! is_admin()){
      wp_nav_menu( array( 'theme_location' => 'topnav', 'container_class' => 'topnav-menu' )    );
   }
   ?>
           
            <?php do_action( '__before_header' ); ?>



               <header class="tc-header clearfix row-fluid" role="banner">
                 
                  <?php do_action( '__header' ); ?>

            </header>

            <?php
             //This hook is filtered with the slider
            do_action ( '__after_header' )
            ?>
0
 
deitercat2Author Commented:
Ace, my appologies, I discovered in my previous code snippet.  I have the call to the wp_nav_menu function twice, once before the body() class and another after the body() class.

Here is the header.php code as it stands now on both development and production:

<?php
/**
 * The Header for Customizr.
 *
 * Displays all of the <head> section and everything up till <div id="main-wrapper">
 *
 * @package Customizr
 * @since Customizr 1.0
 */
?>
<!DOCTYPE html>
<!--[if IE 7]>
<html class="ie ie7" <?php language_attributes(); ?>>
<![endif]-->
<!--[if IE 8]>
<html class="ie ie8" <?php language_attributes(); ?>>
<![endif]-->
<!--[if !(IE 7) | !(IE 8)  ]><!-->
<html <?php language_attributes(); ?>>                          
<!--<![endif]-->
<?php tc__f('rec' , __FILE__ , __FUNCTION__ ); ?>



      <?php do_action( '__before_body' ); ?>

      <body <?php body_class(); ?> itemscope itemtype="http://schema.org/WebPage">
     
       <?php
               if(! is_admin()){
            wp_nav_menu( array( 'theme_location' => 'topnav', 'container_class' => 'topnav-menu' )    );
               }
              ?>
           
            <?php do_action( '__before_header' ); ?>

               <header class="tc-header clearfix row-fluid" role="banner">
                 
                  <?php do_action( '__header' ); ?>

            </header>

            <?php
             //This hook is filtered with the slider
            do_action ( '__after_header' )
            ?>
0
 
deitercat2Author Commented:
Ace, it works!  Not sure where I was copying files to on my production server, but doctype is now above the menu.

Social Icons are not showing up in footer an nav menu above main menu.
0
 
deitercat2Author Commented:
I have discovered that the Social Icons show up when I put the domain name instead of the IP address into the URL.   Thanks again.
0
 
deitercat2Author Commented:
Ace, I would give you at least a 1000 points if they would let me. I appreciate your support an resolution to something that has been frustrating me for awhile now.  Thanks also for your patience!
0
 
Chris StanyonCommented:
Excellent - glad you got it working :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.