Post for People living in [getcity] – 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. If we couldn’t determine your area it’s likely the code returned “Your Area” rather than a location.

Edit: We’ve since created our own API for determining location. Check out our other post here.

Update: We’ve removed access to the hostip API because of reliability issues. Use the above link instead

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 [getcity]“, 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 . 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.

The Shortcode function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

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

There’s a shortcode attribute of fallback. Change the text here that’ll render if the API isn’t able to determine your location. By default it’ll display “Your Location”.

Your Post Permalink

Your WordPress Permalink (the slugged URL of your page) 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.

Download the Code

You may download the PHP function here .

Shortt URL for this post:


  1. Marty

    I’ve checked to see how the city would be resolved if I browsed via, 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”);

    • Marty

      I’ve found the 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.

  2. Jim

    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. :)

    • Marty

      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)?

  3. Mahesh

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

    • Marty

      You mean you want to redirect people to certain sites based on IP address… like,, 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.

      • Mahesh

        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

Leave a Reply

Your email address will not be published. Required fields are marked *