Inserire i pulsanti social in modo automatico nei post di WordPress è un'operazione alquanto semplice. Vediamo insieme questa soluzione.
Possiamo definire il seguente codice in functions.php
:
function my_insert_socials( $content ) {
if( is_single() ) {
global $post;
$id = $post->ID;
$url = get_permalink( $id );
$text = get_the_title( $id );
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'medium' );
$img = $thumb[0];
$html = '<div class="socials">';
$html .= '<a href="https://www.facebook.com/sharer/sharer.php?u=' . $url . '" class="facebook"></a>';
$html .= '<a href="https://twitter.com/intent/tweet?url=' . $url . '&text=' . urlencode( $text ) . '" class="twitter"></a>';
$html .= '<a href="http://www.pinterest.com/pin/create/button/?url=' . $url . '&media=' . $img . '" class="pinterest"></a>';
$html .= '<a href="http://www.linkedin.com/shareArticle?mini=true&url=' . $url . '&title=' . $text . '" class="linkedin"></a>';
$html .= '</div>';
return $content . $html;
}
return $content;
}
add_filter( 'the_content', 'my_insert_socials' );
Inserire i pulsanti social solo nei post singoli è spiegabile con una semplice considerazione: i lettori devono poter leggere un post nella sua interezza prima di decidere se merita di essere condiviso o meno.