Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Last active September 1, 2019 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeschinkel/9d7bb9a45ea5e958db8fdcbdb3439e23 to your computer and use it in GitHub Desktop.
Save mikeschinkel/9d7bb9a45ea5e958db8fdcbdb3439e23 to your computer and use it in GitHub Desktop.
Typical example of header.php code use in WordPress templates.

This is in reference to: https://twitter.com/ramsey/status/1167954877814730755

The following header.php file comes directly from a client site, developed by someone else, with all identifying and account information replaced.

Any of the <?php tags could hypothetically be replaced by a tag that would indicate a different set of rules for only that segment of code, e.g. <?p8 could be introduced to support all the strictness that the advocates of strictness desire. This would give them the strictness where they want it and let everyone else continue to stick with <?php for use in PHP 8.

Futher, by using tags we can incrementally refactor and/or we can, for example, choose methods in a class where we want more strictness and for other methods that need less strictness we can still have that, all in the same file and all processed by the same single (future) PHP 8.x engine.

IMO incremental refactoring is the killer language feature; one that not only enables but also encourages refactoring — little by little — to better practices, and one that does not force the entire file or site to be refactored completely before re-deployment.

Note I am not proposing much difference between code that works with <?php and code that works with <?p8; the only difference would be those things that absolutely require a BC break and where there can be no resolution between those who advocate for — euphamistically speaking — the strict father model and the nurturing parent model.

In the above metaphor, the PHP language processor itself is the parent and its model is defined by strictness it requires and/or the lenience it allows.

<?php
/**
* The header for our theme.
*
* This is the template that displays all of the <head> section and everything up until <div id="content">
*
* @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
*/
// This is used to hook in our cookie checks for our first visit modal, do not remove
do_action( 'mysite_cookie_check' );
$cart_url = get_option( 'mysite_cart_url' , 'https://checkout.mysite.com/commonforms/shoppingcart/viewcart.aspx' );
$login_url = get_option( 'mysite_login_url' , 'http://checkout.mysite.com/CommonForms/SignIn/SignIn.aspx?ReturnUrl=' );
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<?php global $is_IE; if ( $is_IE ) : ?>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<?php endif; ?>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="msvalidate.01" content="ABCDEF123456789ABCDEF123456789AB" />
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
<!-- Start Visual Website Optimizer Synchronous Code -->
<script type='text/javascript'>
var _vis_opt_account_id = 123456;
var _vis_opt_protocol = (('https:' == document.location.protocol) ? 'https://' : 'http://');
document.write('<s' + 'cript src="' + _vis_opt_protocol +
'dev.visualwebsiteoptimizer.com/deploy/js_visitor_settings.php?v=1&a='+_vis_opt_account_id+'&url='
+encodeURIComponent(document.URL)+'&random='+Math.random()+'" type="text/javascript">' + '<\/s' + 'cript>');
</script>
<script type='text/javascript'>
if(typeof(_vis_opt_settings_loaded) == "boolean") { document.write('<s' + 'cript src="' + _vis_opt_protocol +
'abcdefghijklm.cloudfront.net/vis_opt.js" type="text/javascript">' + '<\/s' + 'cript>'); }
/* if your site already has jQuery 1.4.2, replace vis_opt.js with vis_opt_no_jquery.js above */
</script>
<script type='text/javascript'>
if(typeof(_vis_opt_settings_loaded) == "boolean" && typeof(_vis_opt_top_initialize) == "function") {
_vis_opt_top_initialize(); vwo_$(document).ready(function() { _vis_opt_bottom_initialize(); });
}
</script>
<!-- End Visual Website Optimizer Synchronous Code -->
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php mysite_include_svg_definitions();
$no_header_nav = get_post_meta( get_the_ID(), '_no_header_nav', true );
$show_breadcrumbs_header = get_post_meta( get_the_ID(), '_show_breadcrumbs_header', true );
if(is_singular('post') || is_post_type_archive('post') || is_category())
{
$path = wp_parse_url( home_url( $_SERVER['REQUEST_URI']));
if(strpos($path['path'], "blog/appraisal") !== false)
{
$blogSection = " appraisal";
$blogType = " appraisal-blog";
}
elseif(strpos($path['path'], "blog/real-estate") !== false)
{
$blogSection = " real-estate";
$blogType = " real-estate-blog";
}
}
?>
<div id="page" class="site<?php esc_html_e($blogType); ?>">
<a class="skip-link screen-reader-text" href="#main"><?php esc_html_e( 'Skip to content', 'rex' ); ?></a>
<?php mysite_do_myorg_group_navigation(); ?>
<?php
if ( function_exists( 'mysite_do_maintenance_banner' ) ) {
mysite_do_maintenance_banner();
}
?>
<header id="masthead" class="site-header<?php esc_html_e($blogSection); ?>">
<!--[if lte IE 10]>
<div id="updateBrowser">
<p>We've noticed your browser is out-of-date. In order to get the best experience from our website, we strongly suggest upgrading your browser. We recommend <a href="https://www.mozilla.org/en-US/firefox/new/" rel="nofollow" target="_blank">Firefox</a>.</p>
</div>
<![endif]-->
<?php if ( ! $no_header_nav ) { ?>
<nav id="mobileNavbar">
<ul class="navbar-nav">
<!-- <li class="nav-item">
<a id="chatCallMobileNav" class="nav-link start-chat" href="#">
<?php mysite_do_svg( array( 'icon' => 'chat' ) ); ?>
Chat
</a>
</li> -->
<li class="nav-item">
<a class="nav-link" href="tel:8003282008">
<?php mysite_do_svg( array( 'icon' => 'phone' ) ); ?>
Call Us
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo $cart_url; ?>">
<?php mysite_do_svg( array( 'icon' => 'checkout-cart' ) ); ?>
Cart
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo $login_url; ?>">
<?php mysite_do_svg( array( 'icon' => 'login' ) ); ?>
Log In
</a>
</li>
</ul>
</nav>
<?php } ?>
<div class="wrap">
<div class="site-branding">
<a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home" class="site-logo"><span class="screen-reader-text"><?php _e( 'MySite Logo', 'rex' ); ?></span></a>
<?php
if ( ! ( is_404() || is_singular( 'post' || $no_header_nav ) ) || $show_breadcrumbs_header ) {
mysite_add_breadcrumbs_action();
}
?>
</div><!-- .site-branding -->
<?php if ( ! $no_header_nav ) { ?>
<button class="menu-show-hide"><span class="screen-reader-text"><?php _e( 'Show or hide the navigation menus', 'rex' ); ?></span><?php mysite_do_svg( array( 'icon' => 'bars' ) ); ?></button>
<nav id="site-navigation" class="main-navigation">
<?php
wp_nav_menu( array(
'theme_location' => 'secondary',
'menu_id' => 'secondary-menu',
'menu_class' => 'menu dropdown menu-secondary',
) );
?>
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_id' => 'primary-menu',
'menu_class' => 'menu dropdown',
) );
?>
</nav><!-- #site-navigation -->
<?php } ?>
</div><!-- .wrap -->
</header><!-- #masthead -->
<?php
if ( is_singular( 'post' ) || is_post_type_archive( 'post' ) ) {
mysite_add_breadcrumbs_action($darkLinks = true);
?>
<div class="category-nav-container">
<?php wp_nav_menu( array(
'menu_class' => 'category-menu',
'container_class' => 'category-menu-module' . $blogSection,
'theme_location' => 'category-page',
) ); ?>
<!-- the nav go button -->
<div class="category-tabs-button-wrap">
<a href="#" class="content-tabs-button button color-moss">
<span class="button-text"><?php esc_html_e( 'Go', 'rex' ); ?></span>
</a>
</div>
</div>
<?php
mysite_go_back_link();
}
?>
<div id="content" class="site-content">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment