Christian Heilmann

Quickie: A scriptless, imageless, no-third party code Twitter share button for WordPress

Tuesday, July 9th, 2013 at 9:43 pm

Pestered by my colleague Jason Weathersby (“you should have a share button on your blog, I don’t want to copy and paste the title and the URL”) I just added a “share on Twitter” button at the end of all my posts here on the blog. I looked at a few plugins and the official buttons and was not impressed as they all meant a lot of external JS and CSS and HTTP requests. That is not needed. So here is a Twitter Share button without any extra resources from the outside.

JS Bin

It is simple enough:

  • The structure of a “share on Twitter” URL is http://twitter.com/share?url={url to share}&text={text to share}&via={twitter name}
  • In WordPress you get the title of the current post in PHP with the_title() and the permalink of the post with the_permalink()
  • Put them together and you are done:

<p class="tweetthis">
  <a href="http://twitter.com/share
           ?url=<?php the_permalink();?>
           &text=<?php the_title();?>
           &via={your twitter name}" target="_blank">
    share on twitter
  </a>
</p>

Add some styling and we’re in the business. Want to add more sharing buttons without JS? Toby Osbourne has a good post on more URL structures of social sites.

Share on Mastodon (needs instance)

Share on Twitter

My other work: