Skip to content

Instantly share code, notes, and snippets.

@topleague
Created September 2, 2017 18:55
Show Gist options
  • Save topleague/5d054f38d2ddfff897dd23da2ab9379b to your computer and use it in GitHub Desktop.
Save topleague/5d054f38d2ddfff897dd23da2ab9379b to your computer and use it in GitHub Desktop.
Code for Adding Twitter Cards
//Code for Adding Twitter Cards
add_action('wp_head', 'add_twitter_cards');
function add_twitter_cards() {
if(is_single()) {
$tc_url = get_permalink();
$tc_title = get_the_title();
$tc_description = get_the_excerpt();
$tc_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), full );
$tc_image_thumb = $tc_image[0];
$tc_author = str_replace('@', '', get_the_author_meta('twitter'));
?>
<meta name="twitter:card" value="summary_large_image" />
<meta name="twitter:site" value="@league_wp" />
<meta name="twitter:title" value="<?php echo $tc_title; ?>" />
<meta name="twitter:description" value="<?php echo $tc_description; ?>" />
<meta name="twitter:url" value="<?php echo $tc_url; ?>" />
<?php if($tc_image) { ?>
<meta name="twitter:image" value="<?php echo $tc_image_thumb; ?>" />
<?php } if($tc_author) { ?>
<meta name="twitter:creator" value="@<?php echo $tc_author; ?>" />
<?
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment