How To Add Twitter Cards To Wordpress

Sharing previews are yet another thing missing from Wordpress which should really be included by now. It’s dead easy to install a plugin and have it all done for you, but why bother with slowing down your site with needless plugins when you can add a tiny bit of code to your functions file.

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’)); ?> Card Validator.

Greg Morris @gr36