Skip to content

Instantly share code, notes, and snippets.

@blainerobison
Created December 3, 2015 23:35
Show Gist options
  • Save blainerobison/5310d3e77755c4d595b6 to your computer and use it in GitHub Desktop.
Save blainerobison/5310d3e77755c4d595b6 to your computer and use it in GitHub Desktop.
Add ShareThis social icons to WordPress
<div class="addthis_sharing_toolbox"></div>
<?php
/**
* Proactively resolve domain name asynchronously
*
* - ShareThis
*
* @link http://dev.chromium.org/developers/design-documents/dns-prefetching Chromium prefetch behavior
* @link https://developer.mozilla.org/en-US/docs/Controlling_DNS_prefetching Firefox prefetch behavior
* @return void
*/
function prefix_dns_prefetch_sharethis() {
// ShareThis
$share_url = ( is_ssl() ? '//ws' : '//w' ) . '.sharethis.com';
echo '<link rel="dns-prefetch" href="' . $share_url . '">' . "\n";
}
add_action( 'wp_head', 'prefix_dns_prefetch_sharethis' );
/**
* Add ShareThis Markup to Footer
*
* Leverages async method
*
* @link http://support.sharethis.com/customer/portal/articles/475260-examples#sthash.pRR1p8fZ.dpbs
* @return void
*/
function prefix_sharethis_markup() {
$share_url = ( is_ssl() ? '//ws' : '//w' ) . '.sharethis.com/button/buttons.js';
$pub_id = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
echo "<script type='text/javascript'>" . "\n";
echo " var switchTo5x=true;" . "\n";
echo " (function() {" . "\n";
echo " var stscr = document.createElement('script'); stscr.type = 'text/javascript'; stscr.async = true;" . "\n";
echo " stscr.src = '{$share_url}';" . "\n";
echo " var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(stscr, s);" . "\n";
echo " stscr.onload = stCB;" . "\n";
echo " stscr.onreadystatechange = function() { if(stscr.readyState=='loaded') { stCB(); } };" . "\n";
echo " })();" . "\n";
echo " function stCB(){" . "\n";
echo " stLight.options({ publisher:'{$pub_id}', doNotHash: true, doNotCopy: true, hashAddressBar: false });" . "\n";
echo " }" . "\n";
echo "</script>" . "\n";
}
add_action( 'wp_footer', 'prefix_sharethis_markup' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment