Skip to content

Instantly share code, notes, and snippets.

@dcondrey
Created April 13, 2014 02:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dcondrey/10566424 to your computer and use it in GitHub Desktop.
Save dcondrey/10566424 to your computer and use it in GitHub Desktop.
Boilerplate markup for basic Wordpress index.php. Integrated with HTML5 semantic markup and Schema.org structured data. Miscellaneous other PHP functions that I use in most of my templates can also be found here.
<?php
function my_scripts_method() {
wp_enqueue_style( 'style', get_stylesheet_uri());
wp_enqueue_style( 'fonts', get_template_directory_uri() . '/css/fonts.css');
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'social', get_template_directory_uri() . '/js/social.js');
wp_enqueue_script( 'blogjs', get_template_directory_uri() . '/js/blog.js');
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
/* Add HTML5_Shiv and Selectivr (combined and minified into ie.js) */
add_action( 'wp_head', create_function( '','echo \'<!--[if lt IE 9]><script src="/js/ie.js"></script><![endif]-->\';'));
/* ---------- CANONICALIZATION ---------- */
function seo_canonical() {
global $post;
remove_action('wp_head', 'rel_canonical');
$canonical = get_bloginfo('url');
if (is_single() || is_page()) {
$canonical = get_permalink($post->ID);
} else if (is_day()) {
$year = get_query_var('year');
$month = get_query_var('year');
$day = get_query_var('day');
$canonical = get_year_link($year, $month, $day);
} else if (is_month()) {
$year = get_query_var('year');
$month = get_query_var('monthnum');
$canonical = get_year_link($year, $month);
} else if (is_year()) {
$year = get_query_var('year');
$canonical = get_year_link($year);
} else if (is_author()) {
$author = get_query_var('author');
$canonical = get_author_posts_url($author);
} else if (is_category()) {
$cat = get_query_var('cat');
$canonical = get_category_link($cat);
} else if (is_tag()) {
$tag = get_query_var('tag_id');
$canonical = get_tag_link($tag);
}
echo '<link rel="canonical" href="' . $canonical . '" />';
}
add_action('wp_head', 'seo_canonical');
/* ---------- SOCIAL LINKS / SITE HEADER FIELDS ---------- */
function custom_settings_api_init() {
add_settings_section('siteSettings','Global Styles','site_settings','general');
add_settings_field('mastheadControl','Site Header Title','masthead_set','general','siteSettings');
register_setting('general','mastheadControl');
add_settings_field('mastheadControl_alt','Site Header Title Prefix','masthead_alt','general','siteSettings');
register_setting('general','mastheadControl_alt');
add_settings_field('mastheadControl_desc','Site Header Title Description','masthead_desc','general','siteSettings');
register_setting('general','mastheadControl_desc');
add_settings_section('socialSettings','Social Links','social_settings','general');
add_settings_field('facebookControl','Facebook','set_facebook','general','socialSettings');
register_setting('general','facebookControl');
add_settings_field('twitterControl','Twitter','set_twitter','general','socialSettings');
register_setting('general','twitterControl');
add_settings_field('instagramControl','Instagram','set_instagram','general','socialSettings');
register_setting('general','instagramControl');
add_settings_field('pinterestControl','pInterest','set_pinterest','general','socialSettings');
register_setting('general','pinterestControl');
add_settings_field('googlepControl','Google+','set_googlep','general','socialSettings');
register_setting('general','googlepControl');
}
add_action('admin_init', 'custom_settings_api_init');
function site_settings() {echo '<p>Specify text and image which are displayed on top of the site.</p>';}
function masthead_set() {echo '<input name="mastheadControl" id="mastheadControl" type="text" value="'. get_option('mastheadControl') .'" />';}
function masthead_alt() {echo '<input name="mastheadControl_alt" id="mastheadControl_alt" type="text" value="'. get_option('mastheadControl_alt') .'" />';}
function masthead_desc() {echo '<input name="mastheadControl_desc" id="mastheadControl_desc" type="text" value="'. get_option('mastheadControl_desc') .'" />';}
function social_settings() {echo '<p>Enter the URL to your social network profile and it will be displayed on the homepage.</p>';}
function set_facebook() {echo '<input name="facebookControl" id="facebookControl" type="text" value="'. get_option('facebookControl') .'" />';}
function set_twitter() {echo '<input name="twitterControl" id="twitterControl" type="text" value="'. get_option('twitterControl') .'" />';}
function set_instagram() {echo '<input name="instagramControl" id="instagramControl" type="text" value="'. get_option('instagramControl') .'" />';}
function set_pinterest() {echo '<input name="pinterestControl" id="pinterestControl" type="text" value="'. get_option('pinterestControl') .'" />';}
function set_googlep() {echo '<input name="googlepControl" id="googlepControl" type="text" value="'. get_option('googlepControl') .'" />';}
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="shortcut icon" href="<?php echo get_template_directory_uri(); ?>/img/favicon.ico" type="image/x-icon" />
<link rel="apple-touch-icon-precomposed" href="<?php echo get_template_directory_uri(); ?>/img/apple-retina.png" sizes="114x114" />
<link rel="apple-touch-icon-precomposed" href="<?php echo get_template_directory_uri(); ?>/img/apple-ipad.png" sizes="72x72" />
<link rel="apple-touch-icon" href="<?php echo get_template_directory_uri(); ?>/img/apple-touch.png" />
<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
<?php if ( is_singular() && get_option( 'thread_comments' ) ) wp_enqueue_script( 'comment-reply' );
wp_head();?>
</head>
<body <?php body_class(); ?> itemscope itemtype="http://schema.org/WebPage">
<header class="site-header" role="banner" itemscope itemtype="http://schema.org/WPHeader">
<div class="wrap">
<hgroup class="title-area">
<h1 class="site-title" itemprop="headline">Site Name</h1>
<h2 class="site-description" itemprop="description">Site Description</h2>
</hgroup>
</div>
</header><!-- /site-header -->
<nav class="nav-primary" role="navigation" itemscope itemtype="http://schema.org/WebPageElement http://schema.org/ItemList">
<ul itemprop="name" content="navigation_menu">
<li class="menu-1" itemprop="additionalType" itemscope itemtype="http://www.schema.org/SiteNavigationElement http://schema.org/itemListElement"><a href="/" title="Home" itemprop="url"></a></li>
<li class="menu-2" itemprop="additionalType" itemscope itemtype="http://www.schema.org/SiteNavigationElement http://schema.org/itemListElement"><a href="/about.php" title="About" itemprop="url"></a></li>
</ul>
</nav><!--/site-navigation -->
<section>
<main role="main" itemscope itemprop="mainContentOfPage" itemtype="http://schema.org/Blog">
<article id="<?php get_the_ID(); ?> " itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
<header>
<h1 itemprop="headline"><a href="http://mysite.com/post" title="Blog Post" rel="bookmark">Blog Post</a></h1>
<time itemprop="datePublished" datetime="2012-01-07T07:07:21+00:00">January 7, 2012</time>
<span class="entry-author" itemprop="author" itemscope itemptype="http://schema.org/Person">
<a href="http://mysite.com/author" class="entry-author-link" itemprop="url" rel="author">
<span class="entry-author-name" itemprop="name">Author Name</span>
</a>
</span>
</header>
<div class="entry-content" itemprop="text">
<p>This is a blog post.</p>
</div>
<footer class="entry-footer">
<span class="cats">Filed Under: <a href="http://mysite.com/the-category" title="View all posts in Category" rel="category">The Category</a></span>
<span class="tags">Tagged With: <a href="http://mysite.com/?tag=tag-1" rel="tag">tag 1</a>, <a href="http://mysite.com/?tag=tag-2" rel="tag">tag 2</a></span>
</footer>
</article><!-- /post -->
<div class="entry-comments" id="comments">
<h3>Comments</h3>
<ol>
<li class="comment" id="<?php comment_ID(); ?>">
<article itemprop="comment" itemscope itemtype="http://schema.org/UserComments">
<header itemprop="creator" itemscope itemtype="http://schema.org/Person">
<span itemprop="name"><a href="http://authorswebsite.com" rel="external nofollow" itemprop="url">Comment Author</a></span>
<time itemprop="commentTime" datetime="2013-09-15T13:22:51+00:00">Sept. 15, 2013</time>
</header>
<p itemprop="commentText">some comment text etc etc...</p>
</article>
</li>
</ol>
</div><!-- /comments -->
<ul class="pagination" role="navigation" itemscope itemtype="http://schema.org/SiteNavigationElement">
<li class="active" itemprop="name"><a class="more" href="/" itemprop="url">1</a></li>
<li itemprop="name"><a rel="next" class="more" href="" itemprop="url">2</a></li>
<li itemprop="name"><a class="more" href="" itemprop="url">3</a></li>
<li itemprop="name"><a class="more next" href="" itemprop="url">Next &gt;</a></li>
</ul>
</main><!-- /content -->
<aside class="widgets" role="complementary" itemscope itemtype="http://schema.org/WPSideBar">
<div class="widget">
<h4>Primary Sidebar Widget</h4>
<p>This is the Primary Sidebar Widget</p>
</div>
</aside><!-- /site-sidebar -->
</section><!-- /section -->
<footer role="contentinfo" itemscope itemtype="http://schema.org/WPFooter">
<div class="copyright" role="contentinfo" itemscope itemtype="http://schema.org/WPFooter">
<a title="Back to top" href="#">Back to top</a>
<span itemprop="description">Slogan.</span>
<a href="#" alt="Title" target="_blank" rel="home"></a> is owned and operated by <a href="#" alt="Company" target="_blank" rel="home" itemprop="copyrightHolder">Company</a>, 2012-<?php echo date("Y") ?></p>
</div>
</footer><!-- /site-footer -->
</body>
</html>
<?php
/* -- Instagram -- */
add_shortcode( 'instagram', 'instagram_embed_shortcode' );
function instagram_embed_shortcode( $atts, $content = null ) {
$str = "";
$result = wp_remote_get( "https://api.instagram.com/v1/media/popular?client_id=123456789123456789123456789" );
if ( is_wp_error( $result ) ) {
$error_message = $result->get_error_message();
$str = "Something went wrong: $error_message";
} else {
$result = json_decode( $result['body'] );
$main_data = array();
$n = 0;
foreach ( $result->data as $d ) {
$main_data[ $n ]['user'] = $d->user->username;
$main_data[ $n ]['thumbnail'] = $d->images->thumbnail->url;
/* Limit iterations to 12 */
if($n++ >= 11) break;
}
foreach ( $main_data as $data ) {
$str .= '<a href="http://instagram.com/'.$data['user'].'" target="_blank"><img src="'.$data['thumbnail'].'" alt="'.$data['user'].' pictures" class="instagram"></a> ';
}
}
return $str;
}
/* /instagram */
?>
<!-- TODO: Widgetize twitter code: -->
<h4>TWITTER</h4>
<a class="twitter-timeline"
href="https://twitter.com/twitterapi"
data-widget-id="438728713174126592"
data-theme="light"
data-link-color="#00ACEE"
data-chrome="noheader nofooter noborders transparent noscrollbar"
data-tweet-limit="3"
data-related="twitterapi,twitter"
data-aria-polite="assertive" lang="EN"></a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<script>var options = {"url": "/style.css"};CustomizeTwitterWidget(options);</script>/* Hackish full style control */
<script>var hideTwitterAttempts = 0;function hideTwitterBoxElements() {setTimeout( function() {if ( $('[id*=twitter]').length ) {$('[id*=twitter]').each( function(){var ibody = $(this).contents().find( 'body' );if ( ibody.find( '.timeline .stream .h-feed li.tweet' ).length ){ibody.find( '.customisable-border' ).css( 'border', 0 );ibody.find( '.footer' ).css( 'visibility', 'hidden' );ibody.find( '.footer' ).css( 'min-height', 0 );ibody.find( '.footer' ).css( 'height', 0 );ibody.find( '.p-nickname' ).css( 'font-size', 0 );ibody.find( '.p-nickname' ).css( 'display', 'none' );ibody.find( '.e-entry-content' ).css( 'margin-top', '-40px' );ibody.find( '.e-entry-content' ).css( 'margin-left', '40px' );ibody.find( '.var-narrow.var-chromeless .tweet' ).css ( 'padding-bottom', '0px' );ibody.find( 'h1.summary' ).replaceWith( '<h1 class="summary"><a class="customisable-highlight" title="Tweets from fundSchedule" href="https://twitter.com/fundschedule" style="color: #FFFFFF;">fundSchedule</a></h1>' );}else{ $(this).hide();}});}hideTwitterAttempts++;if ( hideTwitterAttempts < 3 ) {hideTwitterBoxElements();}}, 1500);}hideTwitterBoxElements();</script>
<!-- /twitter -->
<div class="social-widget">
<div class="social-icons">
<a href="<?php echo get_option('facebookControl'); ?>" target="_blank" rel="external"><img src="/img/facebook.gif" width="40" height="40" alt="" /></a>
<a href="<?php echo get_option('twitterControl'); ?>" target="_blank" rel="external"><img src="/img/twitter.gif" width="40" height="40" alt="" /></a>
<a href="<?php echo get_option('instagramControl'); ?>" target="_blank" rel="external"><img src="/img/instagram.gif" width="40" height="40" alt="" /></a>
<a href="<?php echo get_option('pinterestControl'); ?>" target="_blank" rel="external"><img src="/img/pinterest.gif" width="40" height="40" alt="" /></a>
<a href="<?php echo get_option('general_setting_google'); ?>" target="_blank" rel="external"><img src="/img/google.gif" width="40" height="40" alt="" /></a>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment