Home / Articles / Inbound Marketing / 5 Simple PHP Scripts To Help Increase Website-led Conversions

5 Simple PHP Scripts To Help Increase Website-led Conversions

Users love to receive recognition for their visits.

Think of it: doesn't it make a big difference when you enter a place where nobody even acknowledges your presence compared to a place where the host will greet you personally, even though they don't know yet?

I'm sure it does.

And you will agree that it makes an even bigger difference when it's your visitors whom you are trying to convert into subscribers or customers and not just one-time visitors.

The five scripts in this post were written to add interactivity that will help with website conversions and please users, making them feel acknowledged and not invisible.

Notes about the scripts

  • All scripts are meant to work in a Wordpress-based environment, however they are flexible enough to be implemented on other types of websites (with exception of #3, that is strictly Wordpress).
  • With the exception of script #3, I wrote and tested all scripts myself and my fiancé Simone Cianfriglia kindly reviewed them. They should be error free, but let me know in the comments if you run into issues or have questions.

1. Localized Greetings With Offer

Say you run specific offers for different countries. You will want a user from a specific country to view offers related to their location and language and not general offers.

The following script will greet users from countries you made special offers for while everyone else will be offered your default offer instead:

<?php

$country_code = trim(file_get_contents("http://ipinfo.io/${_SERVER['REMOTE_ADDR']}/country"));
 $links = require('links.php');

function getLink($country, $links) {
 if ($links[$country])
 return $links[$country];
 else
 return $links['default'];
 }

?>

<p>Hello! I see you are located in <?php echo $country_code; ?>!</p>
<p>We have discounts specific to your market!
<a href="<?php echo getLink($country_code, $links); ?>">Want to take a peek in?</a>
</p>

With links.php being a file that contains this code:

// links.php

<?php

return array(
 'default' => 'URL0',
 'IT' => 'URL1',
 'UK' => 'URL2',
 'US' => 'URL3'
 );

?>

This code returns a message like this

Hello user from RU!

We have discounts specific to your market! Want to take a peek in?

With “Want to take a peek in?” linking to the localized offer.

The code detects if the user connected to the page from a Russian IP in this example, and it links the user's geographic location to the country-specific offer page.

A demo screenshot:

demo screenshot

How the code works

  • The $country_code line retrieves the visitor's country from a public database hosted at ipinfo.io
  • $links retrieves the array contained in the links.php file, that associates each country to its offer page URL
  • The function getLink checks if an association exists between a visitor country and its offer link and, if it does, it returns it (you will “echo” (display) it inside the HTML code of the visitor message); if there is no offer for the visitor country, the function returns the default offer.

How to use this script

Create two .php files:

  • localized-greetings.php
  • links.php

containing the code (customized to suit your needs) introduced above.

Upload to the root folder or a subfolder of your website, then add this simple line of code to your sidebar or website page where you want the offer displayed:

<?php include("/path/to/localized-greetings.php"); ?>

Of course, /path/to/ will be the absolute path of your website folder (ask your host for guidance if you can't figure out).

Why it improves conversions

It's easier to make the right CTA convert when the user doesn't have to click around to find them. This script displays the right link (or banner) as soon as the user visits the page. The offer link is there in front of the user's eyes, ready to click and convert.

2. Time-Aware Contact Page

When a visitor hits your Contact page and wants to get in touch, they might know what timezone you are in but they may not be sure what exact time it is in your place nor if you are available to be contacted.

The following script helps because it changes your availability message according to the time a visitor hits your page:

<?php

date_default_timezone_set("Europe/Rome");

$time = time();
$localtime = strftime("%A %d-%b-%Y %T %Z", $time);
$hour = strftime("%H", $time);
 
echo "<p>It's $localtime in my country (Italy). ";
 
if (17 <= $hour && $hour < 19)
 echo "I'm in the office. How can I help you?";
else
 echo "Office closed, sorry! I'm available 17:00-19:00 (5-7 PM) Mon-Fri.";
 
echo "</p>";
?>

The code will output this message if the user visits your page at a time in your country (Italy in this example) when you are not available:

It's 11:48 PM in my country (Italy). Office closed, sorry! I'm available 17:00-19:00 (5-7 PM) Mon-Fri.

Or this message if they visit your page while you are available:

It's 5:48 PM in my country (Italy). I'm in the office. How can I help you?

A demo screenshot:

demo screenshot

How the code works

  • date_default_timezone_set(“Europe/Rome”) tells the server that your default timezone is a specific one and not the server default. This is important because the location of your server may not be the same as your business. When the code retrieves your local time the moment a visitor hits your page, it will use the timezone you specified and not the server default. In this example, I used “Europe/Rome” as a timezone because it's my location (Italy).
  • I have set three variables:
    • $time for the time() function
    • $localtime for the time calculated according to date_default_timezone_set; $localtime uses the strftime function to format the time string. You can choose the formatting; I chose “%A %d-%b-%Y %T %Z”, that means:
      %A – Sunday through Saturday
      %d – 01 to 31
      %b – Jan through Dec
      %Y – Four digit representation of the year
      %T – Time in hour/minutes/seconds
      %Z – The time zone abbreviation
    • $hour to calculate the current hour in my country and check if the current hour belongs to the interval of office availability (5-7 PM in this example)
  • The if/else construct is the logic core of the script: if the time belongs to your business hours range, the code will display “I'm in the office. How can I help you?”; if not, it will print “Office closed, sorry! I'm available 17:00-19:00 (5-7 PM) Mon-Fri.”

How to use this script

Create time-aware-page.php file containing the code above (with your custom edits).

As with the “How to use this script” tutorial for script #1, use the following line of code to call the .php in your page code:

<?php include("/path/to/time-aware-page.php"); ?>

Why it improves conversions

The script will essentially make it easier for yourself to get contact messages or requests only when you are available and not outside of your business hours.

It also makes it easier for users to know if you are available or not, so they know whether they're going to receive a prompt reply to their message or if they have to wait for the next business day.

Overall, a simple script like this can help to keep communications focused during business hours and avoid wait for both you and your users.

3. An Helpful Marketing Addon for Wordpress

Wouldn't it be nice if your WordPress search form looked like this?

What are you looking for?

Be specific! (e.g. “content marketing tools”)

With “Be specific! (e.g. “content marketing tools”)” as the text inside the search field.

There is no PHP code you need to write here, as this is a simple HTML hack of your default WordPress search form — changing the display text for the search field and the button.

You can do this by opening the searchform.php file in your WP installation and looking for the following tag:

<input type="search" class="search-field"
placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder', 'rootstrap' ); ?>"
value="<?php echo esc_attr( get_search_query() ); ?>" name="s">

“Search …” is what you should edit to customize the words inside the search form. In the example used at the beginning of this explanation, the words to replace “Search …” with are “Be specific! (e.g. “content marketing tools”)”.

This small hack will improve user experience and help your users write and send more relevant queries to retrieve the best results in your WordPress database.

However, you can customize your Search form and results page by installing one of the 15 WordPress Search plugins Hongkiat reviewed in 2015.

Why it improves conversions

Not every user knows how to search for what they are looking for. The more guidance they can get during and after the process, the better.

Sometimes a user will run a simple query in the hope to find what they are looking for among the results. This kind of query is unlikely to bring users the results they need, though, so to give users a hint on how to perform a better site search will lead them to get to the content they are looking for sooner and improve their overall experience (a satisfied user is a user who will come back).

4. Unique Blog Post Freebies

Post-specific CTAs are known to work much better than generic CTAs. You can work with content upgrades or you can automate some of your non-list conversion efforts with this simple PHP script for WordPress

to show a unique offer at the end of each post:

<?php
// FREEBIES BY POST

$postOffers = array(
 '1' => 'This post freebie is at URL1',
 '2' => 'This second post freebie is at URL2',
);

function postFreebie($postId,$postOffers) {
 return $postOffers[$postId];
}

$postId = get_the_ID();
$freebie = postFreebie($postId,$postOffers);

echo $freebie;
?>

A demo screenshot:

example post freebie script

I edited ‘This post freebie is at URL1' in the array as

'<p style="border:1px solid #535353;padding:10px;color:#161616;">This post freebie is at
<a href="URL1">URL1</a></p>'

for this demo.

Note that ‘This post freebie is at URL1' will only appears for this ‘Hello World' post and NOT other posts — this is the scope of the script. To make that same freebie offer available for more posts, you have to specify the post ID in the array; for example, if I want ‘This post freebie is at URL1' to also appear for post ID 354, I will add it as below:

$postOffers = array(
 '1' => 'This post freebie is at URL1',
 '354' => 'This post freebie is at URL1',
);

How the code works

  • The variable $postOffers is an array that links each post ID specified in the array (you can view your post IDs by hovering the mouse on the Edit link under each post in http://example.com/wp-admin/edit.php)
  • The function postFreebie() associates the post ID to its related offer and returns it in the code. Note that $postID uses a Wordpress function that calls the current post ID
  • $freebie takes $postID and $postOffers and returns the right offer for each post, which you will ‘echo' (display) with “echo $freebie”

How to use this script

Create a postoffers.php file and upload it to the root of your domain, a subfolder or within your theme folders. Then call the script inside your theme's single.php template after the post content (<?php the_content() ;>) with this line of code:

<?php include("/path/to/postoffers.php"); ?>

Alternatively, you can simply copy and past the entire code into your single.php template (same position).

Why it improves conversions

Like with content upgrades for your subscriber list, downloadable freebies work better if they are unique to a specific post, so that readers of that post will have a chance to dive deeper in their favorite topic, while they might not be interested in another topic you wrote, say, a free e-book for.

A good format for this kind of conversion ad is the leaderboard format, set at a slightly smaller width than your post content.

If your blog runs on WordPress, you can use Electric Studio Download Counter to count download conversions for each freebie. Also, make sure to set Conversion Goals in Google Analytics, Piwik or other analytics software you use to keep track of conversions.

5. Day-Specific Offers

If you run offers specific to certain days of the week (e.g. 20% off your ebook sales on Tuesday and free counseling on Fridays), you will find this script handy:

<?php

date_default_timezone_set("Europe/Rome");

$time = time();
$hour = strftime("%H", $time);

if (strftime("%w", $time) == 0)
     { echo "<p>Special Sunday Offer!</p>"; }

else if (strftime("%w", $time) == 3)
     { echo "<p>Special Wednesday Offer!</p>"; }

else
     {echo "No special offers today"; }

?>

A demo screenshot:

demo screenshot

How the code works

  • For timezone and time, I used the same code from script #2.
  • %w is a strftime() parameter that means “Numeric representation of the day of the week” and it counts Sunday (0) as the starting day of the week (ending with Saturday (6). So “if (strftime(“%w”, $time) == 0)” means “if current day of the week is a Sunday”, then display “Special Sunday Offer!”.
  • The rest of the code is an if/else construct to display different offers for different days of the week. In this case, only two days of the week have special offers (Sunday and Wednesday), while the other days have no special offers attached, so the code will display a “No special offers today” in this example.

How to use this script

See include instructions for the previous scripts.

Why it improves conversions

It's a waste of time for the user (and very frustrating, too) to fall for an interesting offer only to find out it's not available on any given day.

This simple script will rotate your offer ads on a day basis to only show offers that are available on a specific day and feed the user default options on the other days. If the user is interested in today's offer, today is the day that will bring in conversions and no frustrated users (who may not come back to check on the right day).

More Conversion Scripts?

Brian Dean of Backlinko explains how he increased conversions by 785% with simple PHP scripts, plugins and widgets without A/B testing.

Jose Pérez at ConversionXL also shows how personalized marketing can help increase website-led conversions and sales.

Read more:

Photo of author

Article by Luana Spinetti

Keep Reading