Add the Tweet Button to your Blog without JavaScript

A
Published in: Javascript - Twitter - WordPress

Twitter now offers a simple button that you can add to your website to help visitors easily share your content on Twitter. Here’s why this “official” Twitter button may be better than your current solution:

  • It displays the number of times a particular link has been shared on Twitter – this makes it easy for your visitors to quickly guess the popularity of your content.
  • It uses t.co for shortening links (see example). Since t.co is owned by Twitter, your shortened links are likely to stay around for a long long time.
  • When someone shares your page on Twitter, you have an option to recommend him up to two Twitter accounts after he is done sending the tweet. That may help you win new followers who may otherwise not know that you even exist on Twitter.

Insert the Tweet Button in HTML without JavaScript

Twitter offers a JavaScript based button but you can also write the same as a simple HTML link. The advantage is that it won’t add any extra weight to your blog and you’ll also be able to use it in places where JavaScript is not supported (like your email newsletter).

Here’s an example of such a link – Tweet This.

If you are on WordPress (the self hosted version), you can add the following code anywhere in your template (single.php):

<a href="http://twitter.com/share?url=<?php echo urlencode(the_permalink()); ?>&amp;text=<?php echo urlencode(the_title()); ?>&amp;via=blogaccount&amp;related=myaccount">Tweet this</a>

If you are on Blogger, the code will be something like this. Search for the class “post-footer” in the Blogger XML template and copy-paste this snippet somewhere near it.

<a expr:href='"http://twitter.com/share?text=" + data:post.title + "&amp;url=" + data:post.url + "&amp;via=blogaccount&amp;related=myaccount"'>Tweet this!</a>

Remember to replace the word blogaccount with the Twitter handle of your blog (example, labnol_BLOG) and myaccount with your own Twitter handle (example, labnol).

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Javascript - Twitter - WordPress

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube