Post for People living in New York, NY – Visitor Geographic information in WordPress Titles

Did my headline get your attention? The title should have rendered the city you’re in (or one you’re near) based on your geographic location. I’ve tested geographic based headlines on three sites for the last month with click-through-rates increasing by much as 600% – although it averaged about 300% on most days. That’s a massive increase over the standard rates that apply when I use my fairly generic wooden and static titles.

Why did the CTR increase? People are more likely to click on a blog post link when the content is personal and relevant. Most often used on dating and marketing sites with dodgy headlines like “Meet Women in New York, NY “, they have the potential to be used in an unethical manner. Geo-titles should only be used for good!

Use of targeted titles provides an absolute assurance that your page will receive more transient views. Whether or not your post honors your geo-based promise is another question entirely. As always, be sure not to use trickery into forcing page-views.

The PHP code below utilises the API at Hostip.info. I’ve used this service simply because it was the first result that presented itself when I did a quick Google search. You could use anything… and then modify the code as necessary.

Enable Shortcode in the WordPress Title

Copy and paste the following into the file you use for custom functions. Not unlike the code necessary to add shortcode to a WordPress sidebar, this single line will force the title to process your shortcode functions.

// Enable Shortcode in the WP title
add_filter( 'the_title', 'do_shortcode' );

The Shortcode function

Again, simply copy the following function into your theme’s custom functions.php file.

// Get Locaton/City Shortcode from Hostip.info
function getlocation($atts, $content = null) {
extract(shortcode_atts(array(
    'bordertype' => 'solid'
  ), $atts));
  if (!empty($_SERVER['HTTP_CLIENT_IP']))  
     {  
         $ip=$_SERVER['HTTP_CLIENT_IP'];  
     }  
     elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
     //to check ip is pass from proxy  
     {  
         $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];  
     }  
     else  
     {  
         $ip=$_SERVER['REMOTE_ADDR'];
     } 
     $location = file("http://api.hostip.info/get_html.php?ip=$ip");
// $country = $location[0];
$city = $location[1];
$city = str_replace("City: ", "", "$city");
return $city;  
}
add_shortcode('getcity','getlocation');

The shortcode of [getcity] will render as the city (in your case, New York, NY ) that’s resolved from your IP address anywhere in your WordPress post or title.

Your Post Permalink

Your WordPress Permalink obviously won’t display shortcode. You must change it so it doesn’t include the [getcity] text and replace it with anything of your choosing.

If you would like some code to render any type of content in your posts or title, let me know and I’ll provide the code. Leave me a comment or send me a message if you encounter problems.

First Name:
Your Email Address:
 




Download: WordPress Titles with Geographical Information
Description: Add geo data to WordPress titles and posts
Author:Marty
Category: PHP code
Date: November 2, 2011



If you liked this article, you may also like:

  1. Get the IP Address of a Visitor with PHP
  2. Our Free IP Address API :: LocateMyIP.info
  3. “This Day in History” Information on Your WordPress Website with Shortcode
  4. Add Your Latest Google Plus Post(s) to Your WordPress Blog (or Generate a Google+ RSS Feed)
  5. Embed Facebook Posts into Your WordPress Post or Page with Shortcode
About Marty

is a passionate web developer from Sydney, Australia. He owns about 600 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. Follow Marty on Twitter or Google+.

Comments

  1. I’ve just noticed I left border color in the attribute array. Initially I applied formatting to the displayed text. Ignore it!

  2. I’ve checked to see how the city would be resolved if I browsed via anonymouse.org, and it rendered (Unknown City) rather than a location.

    I’ve simply added this dodgy code to replace the (Unknown City) with “YOU”, but it would be more prudent to change the nature of the title – perhaps other details?

    $city = str_replace(“(Unknown city)”, “YOU”, “$city”);

  3. home theater melbourne says:

    Thanks for the code I am going to give this a test tonight! :)

    • I’ve found the Hostip.info API (or at least their server) to be a little unreliable at times. I’ll build my own IP Location API when I find the time.

  4. I was wondering If you could please help me out?

    I need a shortcode that I can use in my wordpress titles that will display a specific text based on a calendar date.

    For instance on Fathers Day, I would like the shortcode to add something like “Happy Fathers Day” in the whole sites page/post titles, then on another date that I pick I would like the shortcode to display “Happy Halloween” in the wordpress page titles for the whole site.

    If you could help me out I would really appreciate it. I’m looking around your site now to see If you already have some shortcode like what I need. Thanks. :)

    • It would be relatively easy to apply a filter to the title based on dat, but wouldn’t that be over the top? Maybe you could just add a message to the top of the page (or something similar)?

  5. Hi,
    Thanks !Can you give me the code I want to serve 5 pages to a specific country and other pages for global visitors.

    • You mean you want to redirect people to certain sites based on IP address… like au.domain.com, usa.domain.com, uk.domain.com etc? Or do you just want to serve one site to people in, say, USA and redirect everybody else to another site? You’ll have to be very specific.

      • No Marty,
        I want to have one domain and one site say 8 pages out of which 4 served to visitors from one country and the 4 for all other visitors.
        The whole site will be in English
        Thanks

  6. I have (Unknown City) in the title. Shouldn’t there be a fallback of ‘Your area’ or similar?

Please leave a comment or question!

*