Set-up Twitter for Websites

Updated on Wed, 2014-06-11 05:06

The easiest way to create a Twitter for Websites widget—a Tweet button, Follow button, embedded Tweet or timeline—is to use our configuration tools, then copy and paste the generated HTML code into the template for your site.

Embedded Tweets can be copied from the “More •••” menu of any Tweet.

For best performance and reliability, include the widgets.js script in your template

If your site is using multiple widgets you can set up Twitter widgets in your pages once, which will make your site faster, and widgets such as embedded Tweets will be more reliable for authors when using content management systems.

Include the following script in your page template, ideally at the end, before the </body> tag. You do not need to include the script any other time in the page.

  1. <script>window.twttr = (function (d, s, id) {
  2.   var t, js, fjs = d.getElementsByTagName(s)[0];
  3.   if (d.getElementById(id)) return;
  4.   js = d.createElement(s); js.id = id; js.src= "https://platform.twitter.com/widgets.js";
  5.   fjs.parentNode.insertBefore(js, fjs);
  6.   return window.twttr || (t = { _e: [], ready: function (f) { t._e.push(f) } });
  7. }(document, "script", "twitter-wjs"));</script>

This script will inject widgets.js once, asynchronously into your page. This means the script won't block other scripts in your page, or make repeat HTTP requests for the file, and supports all of our widgets, plus Web Intent Events.

Ignore script tags from embeds

If you put the Twitter script into your page template, you don't need to include every <script> tag from the configurator or embedded Tweets dialog. You can just delete them from your articles.

If you use the OEmbed API to include Tweets in your content, you can generate embed codes that don't include the <script> tag by passing omit_script=true in the query string.