{"id":538,"date":"2019-11-19T17:58:58","date_gmt":"2019-11-20T01:58:58","guid":{"rendered":"http:\/\/blog.nillsf.com\/?p=538"},"modified":"2019-11-19T17:59:07","modified_gmt":"2019-11-20T01:59:07","slug":"getting-twitter-to-load-previews-of-your-website","status":"publish","type":"post","link":"https:\/\/blog.nillsf.com\/index.php\/2019\/11\/19\/getting-twitter-to-load-previews-of-your-website\/","title":{"rendered":"Getting Twitter to load previews of your website"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This post doesn&#8217;t cover anything related to Azure or Kubernetes, my typical topics. However, I spent some time over the weekend diving into this topic and I hope to help out some people with this knowledge.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ve been annoyed at Twitter for a while. For the life of me, it wouldn&#8217;t load any preview cards of any of my posts I made on my blog. I circumvented this for a while by manually uploading images, but that wasn&#8217;t the same as the actual URL preview.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I finally decided to dive into it, and the solution was easier than I thought once I figured out what I needed. Maybe this can help you out as well.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Twitter cards<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When Twitter loads a preview of a page, it actually loads something called a <a href=\"https:\/\/developer.twitter.com\/en\/docs\/tweets\/optimize-with-cards\/overview\/abouts-cards\">Twitter card<\/a>. To load these cards, your site should contain some additional metadata. Examples of this metadata are:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;meta name=\"twitter:card\" value=\"summary_large_image\" \/>\n&lt;meta name=\"twitter:url\" value=\"https:\/\/blog.nillsf.com\/index.php\/2019\/11\/17\/privately-connecting-to-paas-services-using-azure-private-link\/\" \/>\n&lt;meta name=\"twitter:title\" value=\"Privately connecting to PaaS services using Azure Private Link\" \/>\n&lt;meta name=\"twitter:description\" value=\"Integrating Azure PaaS services with a private network has been a hot topic. The issue that most customers deal with when it comes to Azure PaaS services is that some of those are published using a public network connection. A good example here is SQL Azure: While databases always have been very well guarded, suddenly \" \/>\n&lt;meta name=\"twitter:image\" value=\"https:\/\/blog.nillsf.com\/wp-content\/uploads\/2019\/11\/private-endpoint1.png\" \/>\n&lt;meta name=\"twitter:site\" value=\"@nillsf\" \/><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">While experimenting with this, you can use the <a href=\"https:\/\/cards-dev.twitter.com\/validator\">Twitter card validator.<\/a> You add the metadata to your webiste, and then use the validator. The validator loads these tags, and shows you preview without you having to keep tweeting. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"557\" height=\"628\" src=\"\/wp-content\/uploads\/2019\/11\/image-39.png\" alt=\"\" class=\"wp-image-539\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-39.png 557w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-39-266x300.png 266w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><figcaption>The Twitter Card validator in action.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Integrating dynamic Twitter card metadata into WordPress<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After a little bit of Googling with Bing, I stumbled across <a href=\"https:\/\/www.wpbeginner.com\/wp-tutorials\/how-to-add-twitter-cards-in-wordpress\/\">this post<\/a> that contained some PHP code that would help with dynamically creating card metadata. This worked for the most part, except that my featured image wouldn&#8217;t load. So I updated that code snippet a little to actually allow me to load my featured Image.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I embedded this code into the <code>header.php<\/code> file of my theme. Just remember that if you ever update your theme, that you need to re-add this code in the file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n#twitter cards hack\nif(is_single() || is_page()) {\n  $twitter_url    = get_permalink();\n $twitter_title  = get_the_title();\n $twitter_desc   = get_the_excerpt();\n    $prefix = 'https:\/\/blog.nillsf.com';\n\t$twitter_thumb  = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );\n\t$twitter_thumburl = $prefix.$twitter_thumb;\n      if(!$twitter_thumb) {\n      $twitter_thumburl = 'https:\/\/pbs.twimg.com\/profile_images\/966627563228553216\/FVNkkIcj_400x400.jpg';\n    } \n  $twitter_name   = str_replace('@', '', get_the_author_meta('twitter'));\n?>\n&lt;meta name=\"twitter:card\" value=\"summary_large_image\" \/>\n&lt;meta name=\"twitter:url\" value=\"&lt;?php echo $twitter_url; ?>\" \/>\n&lt;meta name=\"twitter:title\" value=\"&lt;?php echo $twitter_title; ?>\" \/>\n&lt;meta name=\"twitter:description\" value=\"&lt;?php echo $twitter_desc; ?>\" \/>\n&lt;meta name=\"twitter:image\" value=\"&lt;?php echo $twitter_thumburl; ?>\" \/>\n&lt;meta name=\"twitter:site\" value=\"@nillsf\" \/>\n&lt;?\n  if($twitter_name) {\n?>\n&lt;meta name=\"twitter:creator\" value=\"@&lt;?php echo $twitter_name; ?>\" \/>\n&lt;?\n  }\n}\n?><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This made it work for me, I hope it does for you as well.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;ve ever been annoyed with Twitter not loading previews of your website, you&#8217;ll want to add Twitter card metadata to your web pages. If you&#8217;re hosting your website in WordPress, you can add some code to your header file to dynamically create this metadata.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post doesn&#8217;t cover anything related to Azure or Kubernetes, my typical topics. However, I spent some time over the weekend diving into this topic and I hope to help out some people with this knowledge. I&#8217;ve been annoyed at Twitter for a while. For the life of me, it wouldn&#8217;t load any preview cards [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":541,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[56],"tags":[57,9],"class_list":["post-538","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-twitter","tag-wordpress"],"jetpack_featured_media_url":"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/2019-11-19-17_58_21-Presentation2-PowerPoint.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/comments?post=538"}],"version-history":[{"count":2,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/538\/revisions"}],"predecessor-version":[{"id":542,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/538\/revisions\/542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/media\/541"}],"wp:attachment":[{"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/media?parent=538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/categories?post=538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nillsf.com\/index.php\/wp-json\/wp\/v2\/tags?post=538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}