Skip to content

Instantly share code, notes, and snippets.

@diomededavid
Created July 18, 2021 00:59
Show Gist options
  • Select an option

  • Save diomededavid/3ce717587cbb65116609cec66d9b3c29 to your computer and use it in GitHub Desktop.

Select an option

Save diomededavid/3ce717587cbb65116609cec66d9b3c29 to your computer and use it in GitHub Desktop.
Twitter Schema Plugin
<?php
/*
Plugin Name: Twitter Schema
Plugin URI: https://digitalblender.co
Description: Twitter Schema
Author: Prahlad Yeri
Version: .1
Author URI: https://prahladyeri.com/blog
*/
function add_twitter_card_header()
{
#twitter cards hack
global $post;
if (is_single() || is_page()) {
$twitter_url = get_permalink();
$twitter_title = get_the_title();
$content = get_extended($post->post_content);
$attch_id = get_post_thumbnail_id($post->ID);
$twitter_thumbs = wp_get_attachment_image_src($attch_id, 'full');
$twitter_thumb = $twitter_thumbs[ 0 ];
if (!$twitter_thumb) {
$twitter_thumb = 'https://secure.gravatar.com/avatar/3f253507b82dd33f352c08f649caaa54?rating=PG&size=75';
}
?>
<meta name="twitter:card" content="summary"/>
<meta name="twitter:site" content="@twitterhandle"/>
<meta name="twitter:creator" value="@twitterhandle"/>
<meta name="twitter:url" content="<?php echo $twitter_url; ?>"/>
<meta name="twitter:title" content="<?php echo $twitter_title; ?>"/>
<meta name="twitter:description" content="<?php echo esc_html($content[ 'main' ]); ?>"/>
<meta name="twitter:image" content="<?php echo $twitter_thumb; ?>"/>
<?php
}
}
add_action('wp_head', 'add_twitter_card_header');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment