Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

Beginners Guide on How to Add Twitter Cards in WordPress

Last updated on by
Follow WPBeginner on YouTube
Beginners Guide on How to Add Twitter Cards in WordPress

Twitter is slowly rolling out this experimental technology called Twitter Cards which makes it possible for you to attach media experiences to your tweets that link to your content. This very similar to Facebook status. When you paste a link, it adds a preview with title, summary, and thumbnail of your page content. In this article, we will show you how to add twitter cards in WordPress.

How does Twitter Cards work?

When your tweet has a link to a site that has twitter cards enabled, there is an option to view summary. Whenever another user clicks on the tweet, they will see this summary like the screenshot below. You can see the live example by going to our twitter page.

Twitter Cards Preview

The biggest advantage of having twitter cards is that it increases the number of people following your twitter accounts through content attribution. Often people tweet your links without giving you proper attribution. We have noticed this specifically from some popular content curators. They will tweet our articles without any via @wpbeginner or RT @wpbeginner. This is their right, but we/you as publishers miss out specially when other people retweet your content with someone else’s attributes.

For example if @syedbalkhi retweets a post from @wpbeginner without attribution to @wpbeginner. Then another person retweets @syedbalkhi others are more likely to follow @syedbalkhi rather than following @wpbeginner.

Often content curators do this to keep the tweet lengths short and ensure retweets of their own tweets. With twitter cards, this problem is solved because you get credit to your site from every tweet that mentions your article. Note: we are not picking on content curators. This is a flaw of 140 character limit, and twitter is helping fix it with Twitter cards.

For example when Annie tweeted out our article, she didn’t mention @wpbeginner. But because of proper twitter cards in WordPress setup, we get full credit and exposure. Notice how it has a one click follow button as well.

Twitter Cards Preview Retweet

Now that you know the benefits of twitter cards, lets take a look at how to implement in WordPress.

Video Tutorial

If you don’t like the video or need more instructions, then continue reading.

How to Add Twitter Cards in WordPress

There are two ways of adding twitter cards in WordPress. Let’s take a look at both of them.

Method 1: WordPress SEO by Yoast Plugin

If you are using WordPress SEO by Yoast plugin, then you are in luck. Yoast has built-in integration for twitter cards, so turning it on is a matter of few clicks. Go to the “Social” page of WordPress SEO by Yoast settings page. Check the box for Add Twitter card meta data. Insert your site’s twitter username.

WordPress SEO by Yoast - Twitter Cards Settings

Next thing you need to do is go to your user profile page, Users » My Profile. You will see a new field under Contact Info for your twitter username. Enter your twitter username there. If you are running a multi-author site, then advise your authors to complete this part in their profile. This way each specific author gets credit for their work as well. See the example below:

Twitter Cards Preview - Author

Notice how it says by David Pegg @iamdpegg, and it also has @list25 as the publisher website.

This method is by far the easiest method. Since we use WordPress SEO by Yoast on all of our websites, we are using this method.

Method 2: Hard Code it yourself

Method 2 requires editing some code. Open your header.php file and paste the following code before the closing of the head tag.

<?php
#twitter cards hack
if(is_single() || is_page()) {
  $twitter_url    = get_permalink();
 $twitter_title  = get_the_title();
 $twitter_desc   = get_the_excerpt();
   $twitter_thumbs = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), full );
    $twitter_thumb  = $twitter_thumbs[0];
      if(!$twitter_thumb) {
      $twitter_thumb = 'http://www.gravatar.com/avatar/8eb9ee80d39f13cbbad56da88ef3a6ee?rating=PG&size=75';
    }
  $twitter_name   = str_replace('@', '', get_the_author_meta('twitter'));
?>
<meta name="twitter:card" value="summary" />
<meta name="twitter:url" value="<?php echo $twitter_url; ?>" />
<meta name="twitter:title" value="<?php echo $twitter_title; ?>" />
<meta name="twitter:description" value="<?php echo $twitter_desc; ?>" />
<meta name="twitter:image" value="<?php echo $twitter_thumb; ?>" />
<meta name="twitter:site" value="@libdemvoice" />
<?
  if($twitter_name) {
?>
<meta name="twitter:creator" value="@<?php echo $twitter_name; ?>" />
<?
  }
}
?>

Source: Ryan Cullen @artesea

If you don’t know how to work with PHP or running into issues with Method 2, then please use Method 1.

Final Step: Apply to Participate

Just because you added the twitter card support on your website does not mean that it will appear on the twitter website. Quote from the official twitter cards documentation:

We’re in the process of bringing this new content preview experience to users on Twitter. And over the coming weeks we want to generate content previews for more sites from around the web. To participate in the program, you should (a) read the documentation below, (b) determine whether you wish to support Twitter cards, and then (c) apply to participate.

So it is clear that you have to apply to have this activated for your site. We applied for WPBeginner, and it was approved relatively fast. Here is what you need to do to apply:

  1. Test your Site with the Preview Tool
  2. Fill out this Form to apply

In the application form, you don’t have to submit the photo card URL and player card URL. Just submit the example summary card URL, which can be any single post on your website.

That’s it. Enjoy your twitter cards in WordPress integration. If you have thoughts, questions, and suggestions, feel free to comment. If you enjoyed this article, then consider following us on twitter @wpbeginner


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • http://www.adventuresofnobleandpond.com/ Megan Kubasch

    So I have installed WP SEO by Yoast, and I have followed all of the instructions up to the point where I insert the Card URL on the Preview page for the Card Validator on Twitter. What URL am I supposed to Insert? I have used my URL for my blog, but it comes up with an error, saying No Card Found (Card Error). Any help you can provide would be greatly appreciated.

  • http://www.bubble-jobs.co.uk/ Lauren Riley

    This is really useful thank you.

    One thing, we have enabled this using the WordPress Yoast SEO Plug-in and set up a Twitter card manually entering all of the information on the card validator for one blog post.

    This worked, however when I tweet a link to my other blog posts it doesn’t pull through the Twitter card. Do you have to set up the Twitter cards for each blog post or should it do it automatically for each blog post?

  • http://www.ungeekencolombia.com/ Manuel Echeverry

    graciass!!! thank you! finally I managed to submit my request to twitter, hopefully i will get my card approved

  • Ruth

    Oh, and ps
    Do we have to approve each post, or will that be automatic after our first post was approved… Again, thank you

  • Ruth

    Thanks, it works great, except for one thing: I can’t get an image to be included. How would I do that,

  • Karan Singh

    when i install this plugin then it is asking for “You’ve just installed WordPress SEO by Yoast. Please helps us improve it by allowing us to gather anonymous usage stats so we know which configurations, plugins and themes to test with” Allow Tracking or not, what should i do,
    Allow tracking or not.

  • Sri Ganesh.M

    The codings are not working for animhut blog. Showing error ! Invalid card type

  • Kevin

    Hello there. Great guide, thanks!

    I’m currently stuck trying to validate twitter:creator. When I look at the source for one of my posts, I don’t see the twitter:creator meta tag at all. Would you mind, pointing me in the right direction? I’m hard-coding it.

  • Gareth

    Great post – i was struggling to set up twitter cards using a wordpress plug in without realising that Yoast had it in-built!

    All set up and awaiting approval – thanks guys

  • Jason Acidre

    Useful post! Been planning to add Twitter cards on my blog for ages now, and just had the time to tweak it earlier, found this guide very helpful, so thanks.

  • Reap3RGHS

    Hello,
    I having a simple problem. I going to add my twitter username to my profile settings. I just add Reap3R_GHS and turn it into http://reap3r_ghs. What can I do?

    • http://www.wpbeginner.com Editorial Staff

      That’s weird. Did you post it in the support forum for Yoast to see?

      • Reap3RGHS

        Isn’t in Yoast settings but in profile settings…

  • Julien Maury

    Hi,

    It’s great but because we call the excerpt outside the loop (header), people could get bad surprises for their description.

    So here is a good snippet from uplifted.net that fixes the problem :

    function get_excerpt_by_id($post_id){
    $the_post = get_post($post_id); //Gets post ID
    $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
    $excerpt_length = 35; //Sets excerpt length by word count
    $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
    $words = explode(‘ ‘, $the_excerpt, $excerpt_length + 1);
    if(count($words) > $excerpt_length) :
    array_pop($words);
    array_push($words, ‘…’);
    $the_excerpt = implode(‘ ‘, $words);
    endif;
    $the_excerpt = ” . $the_excerpt . ”;
    return $the_excerpt;
    }

    Then you might replace get_the_excerpt() with this :
    get_excerpt_by_id($post_id)

    Thanks. :)

    • Julien Maury

      Sorry that wasn’t exactly that :

      Call the function this way : get_excerpt_by_id($post->ID) otherwise you’ll get notice !
      $post_id is undefined.

      Plus get_the_excerpt() is deprecated.

      You can also add an esc_attr() on $the_excerpt to avoid broken meta if the excerpt has quotes

      Thanks !

    • Main Uddin

      There is no needs to use any code , simply use WordPress SEO by Yoast as Syed Balkhi has said which is the best for Twitter card

      • Julien Maury

        Hi,
        There’s no need to use any code. But still my plugin allows you to choose which type of card you want to use on each post. Moreover you can change meta creator (guest blogging) per each post too. I think it’s worthy :)

        • Roy McKenzie

          Totally Worthy!

  • Manuel Garcia

    When I preview my twitter card, the error says:
    “Internal Error. Most likely an fetcher error.”
    What to do?

  • Sai Liou

    Thank you for the article! I went with method #1. In the twitter preview, I’m only seeing the summary of the post and not the image. I’ve updated the robot.txt file to allow twitterbot. However, it’s still not showing any image in the preview. Any thoughts on this? Thank you in advance.

    • http://www.wpbeginner.com Editorial Staff

      Look in your view:source to see if you have the image tags. The image is usually pulled from your featured image.

  • Mattia Frigeri

    Good article. I activated the summary card…

    Anyway: how do you change the anchor text name of the website in the attribution below?
    For instance yours is “WordPress Beginner”, other sites use “example.com”. In my case it uses my name. How do you manage it? I’d like to show a piede of my URL like “example.com”

  • David Benson

    Just wanted to let you know that this post saved me HEAPS of time trying to get this to work. Clear, clean and to the point walkthrough. It’s very much appreciated.

    A followup question. I added all the steps to my blog, TheSocialChic.com, but I’m curious as to how much time it usually takes for them to turnaround an approval process. Will I get an email or anything when I (or if) I am approved for Twitter cards? No worries if you don’t know, but I greatly appreciate any guidance on this topic.

    Thanks again for such a helpful post. Keep up the good work and I will be definitely staying tuned to all future posts.

    • http://www.wpbeginner.com Editorial Staff

      Approval time varies. In our experience, twitter cards were enabled on our account before we received an email from twitter. But yes, we did receive an email from twitter.

      • David Benson

        Thanks for letting me know. Very much appreciated. Keep up the great work with this site. I will be staying tuned.

  • Bridie Jenner

    I now have this on my site, and very exciting as the first blog post I tried worked – fantastic!

    But I’ve tried it with a few others and it’s not showing the summary… any ideas?

    • http://www.wpbeginner.com Editorial Staff

      Did twitter approve your site yet? Remember, they have to approve the site first.

  • Bridie Jenner

    These are great! Just applied to Twitter for mine and will be sharing this. Thanks.

  • John John

    Awesome article, thank you for all these infos.

  • http://twitter.com/frontendboss Parvez Ansari

    I got “Summary Card” working for me by following method 2 for my website which is built usng wordpress. I inserted the code to generate meta data in the single-page wordpress template.

    I want to know “how” and “when” to use “Photo Card”

    • http://www.wpbeginner.com Editorial Staff

      You would use photocards on photo specific pages. For example if you have a photo blog, then it makes more sense for you to use a photocard.

  • http://www.facebook.com/profile.php?id=718653202 Chris Kovalenko

    Setting this up today

  • Urban Renstrom

    Thank for the twitter card tip.

    under my profile my twitter handle was missing, doh…

  • http://twitter.com/ontargetcoach Brent Pittman

    Wow! Thanks for keeping us updated with the latests. It seems like this will save a lot of time and we won’t have to attribute the source since it is done automatically.

  • Lisander

    Thanks for the tutorial.. I was wondering what that twitter card on yoast’s plugin was.
    But now I know, I hope I get accepted.

  • Rakesh M. Pawar

    Hello, thanks for article, we’ll try this code to implement Twitter Cards.
    I’ve one more question (I know this question is not related to this article): How to get such effects like in your post, when we hover mouse over twitter handle it shows popup with follow button, please tell us how to implement that? Thanks in advance.

  • Nino Blasco

    Always good articles, congratulations.
    I’ll try just the code.
    Thank you.