Skip to content

Instantly share code, notes, and snippets.

@slackero
Last active June 15, 2016 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slackero/20386e2cf72045f0ccada015c9b17ba5 to your computer and use it in GitHub Desktop.
Save slackero/20386e2cf72045f0ccada015c9b17ba5 to your computer and use it in GitHub Desktop.
Twitter Cards frontend render script for phpwcms
<?php
/**
* Twitter Cards frontend render script for phpwcms
*
* Use it in combination with [X] social sharing checkbox to mix
* OpenGraph meta tags with Twitter Cards Markup Tags and optional
* HTML comment to define type of Twitter Card (e.g. template)
* like <!--TWITTER:summary_large_image-->
*
* @see https://dev.twitter.com/cards/getting-started Documentation of Twitter Cards
*
* @author Oliver Georgi <slackero@gmail.com>
* @copyright 2016 Oliver Georgi
*/
if(!$content['list_mode']) {
// @username of website
set_meta('twitter:site', '@phpwcms');
// Define the default Twitter Card type
$twitter_card = 'summary';
// Search template for specific card property
if(preg_match('/<!--TWITTER:(.*?)-->/', $content['all'], $match_twitter_cards)) {
// possible Twitter card values
$twitter_cards = array(
'summary' => '',
'summary_large_image' => '',
'photo' => '',
'gallery' => '',
'product' => '',
'app' => '',
'player' => ''
);
if(($match_twitter_cards = trim(strtolower($match_twitter_cards[1]))) && isset($twitter_cards[$match_twitter_cards])) {
$twitter_card = $match_twitter_cards;
};
};
set_meta('twitter:card', $twitter_card);
// Use the article username if matching a Twitter user ID
if($content['article_username'] && substr($content['article_username'], 0, 1) === '@') {
set_meta('twitter:creator', $content['article_username']);
}
/**
* There are much more Twitter Card Markup Tags
* @see https://dev.twitter.com/cards/markup Cards Markup Tag Reference
*/
}
@slackero
Copy link
Author

Example rendering result
bildschirmfoto 2016-06-15 um 08 14 38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment