How To Add “Tweet This” Links To Any CMS With PHP

Twitter is a popular social media platform that not only enables communication but can help drive traffic to your blog. On way to integrate twitter with your blog is to add a “Tweet This” link to your pages. If you are using WordPress or another popular CMS you can find plugins that will do this for you. However, if your CMS is custom built or if you are unable to install plugins you can use the script we are going to build below to integrate twitter into any PHP page.

With this script we are going to follow the following steps.

  1. Set a few parameters for the link.
  2. Determine the URL of the page.
  3. Pass the URL through a shortening service’s API.
  4. Create HTML link to Twitter with tiny URL.

Set a few parameters for the link.
In our first step we are going to add a few parameters to define parts of the hyperlink that the script will be creating. To do this we will be defining two strings, $linktext and $tweetmessage. $linktext is the anchor text of the link that we will be creating and $tweetmessage is the prefix message that will appear in the tweet right before the tiny URL.

/*** To customize edit below ***/

    /*** Link Anchor Text ***/
    $linktext = "Tweet this Article!";

    /*** Message in Tweet ***/
    $tweetmessage = "Currently Reading:";
    $encodedmessage = urlencode($tweetmessage);

Because $tweetmessage will become part of the final URL in our link we want to make sure that it is encoded to work correctly across the HTTP. Therefore we use the urlencode() function to create $encodedmessage.

Determine the URL of the page.
We will be using the $_SERVER array to determine the page’s URL in order to pass through a shortening service. We first detect the URL’s protocol by using $_SERVER[‘HTTPS’]. This will help us determine if the URL is http or https. Next we combine the results from our $_SERVER[‘HTTPS’] check with $_SERVER[‘HTTP_HOST’] and $_SERVER[‘REQUEST_URI’]

    /*** check for https ***/
    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
    /*** return the full address ***/
    $longurl = $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

$longurl now contains the full URL to the page.

Pass the URL through a shortening service’s API.
We now want to pass $longurl through a shortening service so that we don’t tweet a long URL. In this example we are using TinyURL, but you can use any service who’s API returns data in a text format. To grab the tiny URL we will use PHP’s cURL library.

/*** creates URL for API ***/
    $urlapi = "http://tinyurl.com/api-create.php?url=".$longurl;
    /*** activate cURL for URL shortening ***/
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $urlapi);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $shorturl = curl_exec($ch);
    curl_close($ch);

Now $shorturl contains the new shortened URL that we will use in our tweet.

Create HTML link to Twitter with tiny URL.
Here we take all of the variables and combine them to create our HTML “Tweet This” link.

 /*** make the tweet code ***/
    echo ''.$linktext.'';

Installation

  1. Copy the full code below.
  2. Edit code to suit your needs.
  3. Save file and upload to your web host.
  4. Paste below include code in PHP doc where you want link to appear.

Working example.

Include Code

include 'TweetLink.php';

FULL CODE
Download this in a text file here.

/*** To customize edit below ***/

    /*** Link Anchor Text ***/
    $linktext = "Tweet this Article!";

    /*** Message in Tweet ***/
    $tweetmessage = "Currently+Reading:";
    $encodedmessage = urlencode($tweetmessage);

    /*** check for https ***/
    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
    /*** return the full address ***/
    $longurl = $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    /*** creates URL for API ***/
    $urlapi = "http://tinyurl.com/api-create.php?url=".$longurl;
    /*** activate cURL for URL shortening ***/
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $urlapi);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $shorturl = curl_exec($ch);
    curl_close($ch);
    /*** make the tweet code ***/
    echo ''.$linktext.'';

Comments

  1. […] Read more: How To Add “Tweet This” Links To Any CMS With PHP | JOZSOFT […]

Trackbacks

  1. […] this link: How To Add “Tweet This” Links To Any CMS With PHP | JOZSOFT […]

  2. […] More: How To Add “Tweet This” Links To Any CMS With PHP | JOZSOFT […]

  3. […] How To Add “Tweet This” Links To Any CMS With PHP | JOZSOFT […]

  4. […] is the original post: How To Add “Tweet This” Links To Any CMS With PHP | JOZSOFT Share and […]

  5. […] How To Add “Tweet This” Links To Any CMS With PHP | JOZSOFT […]

  6. […] This post was mentioned on Twitter by Joe Hall, Dave Rohrer, Leighanne Stainer, Kristi Johnson, Kristi Johnson and others. Kristi Johnson said: RT @jozsoft: How To Add “Tweet This” Links To Any CMS With PHP http://bit.ly/bh02a6 […]

  7. […] Read more: How To Add “Tweet This” Links To Any CMS With PHP | JOZSOFT […]