Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Special Message for Social Media Referrals

Publish a special message for people who click a link to your page at Facebook or Twitter.

If done professionally (as opposed to being spammy), the message can boost confidence and a feeling of being part of a special group. The type of website and its audience determine what is appropriate and what is not.

An acknowledgment may be appropriate. Perhaps also an invitation to interact even more, as in follow or friend requests. An announcement of upcoming tweets or posts, or a special event, might also work.

Direct selling within the special message may be perceived as spammy by those who spend a lot of time at social media sites.

The Code

The PHP code to publish a special message for Facebook and Twitter people is further below. With instructions.

Similar code can be used for other social media sites in addition to Facebook and Twitter. Instructions for that are also further below.

This code is for non-WordPress PHP pages.

Here is the PHP code (customization notes follow):

<?php
// Twitter section.
$Twitter_ReferralURL_beginning = "http://t.co/";
if( isset($_SERVER['HTTP_REFERER']) and 
   strpos($_SERVER['HTTP_REFERER'],$Twitter_ReferralURL_beginning)===0 )
{
   echo <<<MESSAGE
<div style="border:1px dashed gray; padding:0 5px 0 15px;">
<p>
Hi! <b>Thanks</b> for clicking our Twitter link.
</p>
<p>
If you haven't yet done so, please follow us. 
Lot's more good stuff like you clicked on is forthcoming.
</p>
</div>
MESSAGE;
}

// Facebook section.
$Facebook_ReferralURL_beginning = "https://www.facebook.com/";
if( isset($_SERVER['HTTP_REFERER']) and 
   strpos($_SERVER['HTTP_REFERER'],>$Facebook_ReferralURL_beginning)===0 )
{
   echo <<<MESSAGE
<div style="border:1px dashed gray; padding:0 5px 0 15px;">
<p>
Hi! <b>Thanks</b> for clicking our Facebook link.
</p>
<p>
If you haven't yet done so, please send us a friend request. 
<b>We'll confirm it!</b> 
Also, when you get a chance to do so, click our Like button.
</p>
</div>
MESSAGE;
}
?>

Implementation Instructions

The purple text is the special message for Twitter people and the orange text is for Facebook people.

As you can see, both may contain HTML tags. In fact, both can contain any HTMl and JavaScript code that works on a web page.

Change the purple text to whatever you want to publish for those who click a link to your page from a Twitter tweet.

Change the orange text to what you want to publish for people clicking from Facebook.

Important: The the purple text and the orange text are between lines containing the MESSAGE key words. It's important to keep the MESSAGE key word lines as they are. Put the content to publish in lines between the key word lines, not on the key word lines themselves.

When the changes have been made, paste the PHP code into your web page where you want the special message to be published. (The web page must, of course, be a PHP web page; generally, that means a web page with a .php file name extension.)

Implementing for Additional Social Media Sites

Below is the same code as presented further above, except the coloring is different. Instructions follow the code.

<?php
// Twitter section.
$Twitter_ReferralURL_beginning = "http://t.co/";
if( isset($_SERVER['HTTP_REFERER']) and 
   strpos($_SERVER['HTTP_REFERER'],$Twitter_ReferralURL_beginning)===0 )
{
   echo <<<MESSAGE
<div style="border:1px dashed gray; padding:0 5px 0 15px;">
<p>
Hi! <b>Thanks</b> for clicking our Twitter link.
</p>
<p>
If you haven't yet done so, please follow us. 
Lot's more good stuff like you clicked on is forthcoming.
</p>
</div>
MESSAGE;
}

// Facebook section.
$Facebook_ReferralURL_beginning = "https://www.facebook.com/";
if( isset($_SERVER['HTTP_REFERER']) and 
   strpos($_SERVER['HTTP_REFERER'],$Facebook_ReferralURL_beginning)===0 )
{
   echo <<<MESSAGE
<div style="border:1px dashed gray; padding:0 5px 0 15px;">
<p>
Hi! <b>Thanks</b> for clicking our Facebook link.
</p>
<p>
If you haven't yet done so, please send us a friend request. 
<b>We'll confirm it!</b> 
Also, when you get a chance to do so, click our Like button.
</p>
</div>
MESSAGE;
}

?>

To learn how to implement additional social media sites, first understand how Twitter and Facebook have been implemented.

Twitter

Let's first take the blue text and the purple text. It's for the Twitter links.

You'll notice the blue text in two places, once on the 3rd text line and once on the 5th text line. Those two are the variable name containing the URL to compare when the web page is loaded.

At the 3rd text line, at the first instance of the blue text text, you'll also see some purple text. The purple text is the beginning of the referring URL when a link in a tweet is clicked. When the web page loads, the purple text value is compared with what the browser provides to see if it came from a tweet.

Facebook

The Facebook section is like the Twitter section. Except it contains red text and orange text instead of blue text and purple text.

It works the same as the Twitter section except it notices when the browser arrives because a Facebook link was clicked. That's because the orange text is the beginning of the Facebook referring URL, not the Twitter one.

Both the Twitter and Facebook sections publish the content between the MESSAGE key words when a link at the respective site is clicked to load this web page.

Adding Another Social Media Site

Now that you see how the Twitter and Facebook special messages are implemented, it's possible to add another social media site.

The code from either the Twitter section or the Facebook section could be used to add another social media site. In order to use color references in the following instructions, let's refer to the Twitter section.

Copy the Twitter section and use it to create a new section in the PHP code. Then, modify the new section.

  1. Replace the variable name and its value. That would be the blue text in two places and the purple text in one place.

    To determine the referring URL to replace the purple text with:

    • Create a web page containing this JavaScript.

      <script type="text/javascript">
      if( document.referrer.length ) { document.write(document.referrer); }
      else { document.write("No referrer information available."); }
      </script>
      
      
    • Add content at the social media site that links to the web page with the JavaScript.

    • Click on the link at the social media site. Your browser goes to your web page. When your web page is loaded, the JavaScript has published the referring URL on your page, ready to copy. (Unless your browser's preferences prevent it from providing the referral information.)

      That's the URL you want to use to replace the purple text with – but just the first part – the beginning of the URL through the slash following the domain name.

      For example, if the URL the JavaScript published were http://example.com/page/?something, then the URL to replace the purple text with is http://example.com/

  2. On the lines between the MESSAGE key words, provide the content to publish in the event the web page loads from a click in your content at the new media site.

    As before, the lines containing the MESSAGE key words need to remain as they are. Put the content to publish in lines between the key word lines, not on the key word lines themselves.

With the code for Twitter and Facebook, and with the ability to add other social media sites, you're well set to offer special messages to people who like your content enough to click on your link.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC