Possiamo generare facilmente ed in modo performante i bottoni dei vari social network come Twitter, Facebook e Google Plus usando delle semplici funzioni da aggiungere al nostro tema di WordPress. Vediamo come.
Aggiungete il seguente codice al file functions.php
del vostro tema (se non esiste createne uno):
function tweet_button() {
$title = get_the_title();
$link = get_permalink();
$html = '<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/tweet_button.html?';
$html .= 'url=' . $link . '&text=' . $title . '&count=vertical&via=username"></iframe>';
return $html;
}
function facebook_button() {
$title = get_the_title();
$link = get_permalink();
$html = '<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://www.facebook.com/plugins/like.php?';
$html .= 'href=' . $link . '&title=' . $title . '&layout=box_count"></iframe>';
return $html;
}
function google_plus_button() {
$link = get_permalink();
$html = '<iframe src="https://plusone.google.com/u/0/_/+1/fastbutton?url=';
$html .= $link . '&size=tall&count=true&annotation=bubble&lang=it" scrolling="no" frameborder="0"></iframe>';
return $html;
}
Quindi richiamate le funzioni nel modo seguente dove volete che compaiano i bottoni:
<div id="social">
<span class="twitter"><?php echo tweet_button();?></span>
<span class="facebook"><?php echo facebook_button();?></span>
<span class="google-plus"><?php echo google_plus_button();?></span>
</div>
Tenete presente che le funzioni devono sempre trovarsi nel Loop di WordPress.