New Plugin: Ads By Country (Javascript version)

Hi gang,

I've just written a plugin to show ads (HTML code) to site visitors (as sidebar widgets) based on which country they're in.

From the US? ==> You get the US ad
From the UK? ==> You get the UK ad
From Australia? ==> You get the AU ad

It's been possible to do this using "Who Sees Ads" (or WSA) except that WSA doesn't work when you've got caching turned on. The WSA logic is all written in PHP.

I had a requirement to use W3 Total Cache. So I wrote this plugin using Javascript only. It uses the (free) MaxMind GeoIP JS Web Service to return the user's IP/Country using only Javascript.

Anyone want to test it and/or look at my code?

I'm not a great coder, so I may be making some mistakes...

1. There is an "Ads by Country" options page where you put in your ads (your HTML Code). That code gets saved into the WP database (wp_options table).

2. Should I create a different entry in the DB for each country, or combine all this into one array?

3. Should I implement some kind of simple caching? Right now, every time the user loads the page, it uses the WP "get_option" function to pull the ad code from the DB. I'm thinking each user only needs to pull that from the DB once.

4. Does it need some error checks?

Above all, I'm trying to reduce the impact on the server by being smart about using the DB.

  • Mason
    • DEV MAN’s Sidekick

    Hiya nickd32!

    Well, first off, thanks for sharing! :slight_smile:

    I'm gonna ask a couple of our developers to drop in and see if they can take a look and give you some pointers. I will note that they're crazy busy at the moment as we're down a man right now.

    What do you guys think? Barry, Ivan, Aaron? Any thoughts on this one?


  • Ivan
    • The Incredible Code Injector

    Hi Nick,

    I have gone trough the code and it looks good. Maybe some formating and indentation will improve the readability. Also you haven't localized all the stings.

    Re your questions:

    1. This one looks alright.

    2. For this particular thing I would recommend storing all entries into a single array.

    3. The get_option function has an internal cache so you don't need to cache anything explicitly.

    4. Yeah, it will be good to verify _wpnonce on form submission.

    Keep up the good work :wink:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.