Creato da Facebook, Open Graph è un protocollo che consente ai blog o ai siti web di “comunicare” con il social network. Di seguito troverete un codice super-utile da aggiungere al vostro blog WordPress per renderlo, senza installare alcun plugin, pienamente conforme Open Graph.
Si tratta semplicemente di incollare il seguente codice all’interno del file functions.php.
Ciò aggiungerà automaticamente i metadata Open Graph nella sezione
<head> delle vostre pagine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function wptuts_opengraph_for_posts() { if ( is_singular() ) { global $post; setup_postdata( $post ); $og_type = '<meta property="og:type" content="article" />' . "\n"; $og_title = '<meta property="og:title" content="' . esc_attr( get_the_title() ) . '" />' . "\n"; $og_url = '<meta property="og:url" content="' . get_permalink() . '" />' . "\n"; $og_description = '<meta property="og:description" content="' . esc_attr( get_the_excerpt() ) . '" />' . "\n"; if ( has_post_thumbnail() ) { $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' ); $og_image = '<meta property="og:image" content="' . $imgsrc[0] . '" />' . "\n"; } echo $og_type . $og_title . $og_url . $og_description . $og_image; } } add_action( 'wp_head', 'wptuts_opengraph_for_posts' ); |
Per rendere il vostro blog infine conforme al protocollo Open Graph, sostituite, all’interno del file header.php, il tag <head> con il seguente:
1 |
<html <?php language_attributes(); ?> prefix="og: http://ogp.me/ns#"> |
Buon lavoro!