0

I want to retrieve the user's location and then show a link that points to an URL that changes according to that location. I just want to place the user's city name at the end of the HREF.

I need this work on my wordpress site, on a static page. I use a plugin called Exec-php which let's me run PHP in pages.

I have a plugin that provides me the user's city through the shortcode "[mmjs-city]".

I tried to make it work through different paths but I never get the link to work.

Here I tried assigning that shortcode to a value,

<?php 
$city= "[mmjs-city]";
echo $city;

echo "<a href='?s=" . $city ."'>Search for your city</a>";
?>

I added the first two lines to check whether the shortcode is working or not and if it's correctly assinged to the value $city. That part works. Then it creates the link and put's the value $city at the end of it. But when trying it instead of taking me to:

/?s=new+york

It takes me to:

/?s=%3Cscript%20language=%22javascript%22%3Edocument.write(geoip_city());%3C/script%3E

I have no idea what to do. I would be really thankful for any info on how to make it work, it's really an important feature for my site. Please ask for any further info or idk anything.

Also this is where I tested that code:

http://chusmix.com/?page_id=1129

Thanks

4
  • isn't this a javascript shotcode? Dec 25, 2010 at 22:27
  • It stated that I'm in beirut above the link and the link parameter was empty: chusmix.com/?s=
    – ifaour
    Dec 25, 2010 at 22:32
  • Yes, I'm using the plugin WP-Geolocation-JS. I forgot to mention it Dec 25, 2010 at 22:33
  • Sorry, I had mispelled the value. If you check it again you'll see it directs to that long URL I posted above. Dec 25, 2010 at 22:37

4 Answers 4

3

The problem is that the $city actually outputs a piece of javascript. The browser then executes the function and this writes the user's city to the screen.

<script language="javascript">document.write(geoip_city());</script><br/><br/><a href='?s='>Search for your city</a>

If you use this in your href, this will literally link to <script language="javascript">document.write(geoip_city());</script> which causes the URL as you show.

You are using Javascript and PHP through eachother. If you want to create a link with the city, have the link created (or be modified by) javascript: locate the link (give it an ID or find it in the DOM) and assign the href, or print the link itself using javascript (instead of the A):

<script language="javascript">
document.write('<a href="?s=' + geoip_city() + '">Search for your city</a>');
</script>
1
  • Thanks an AWFUL lot. I lov you Dec 25, 2010 at 23:14
1

If you check the HTML-code of the page you'll probably see the

<script language="javascript">document.write(geoip_city());</script>

which is interpreted as JavaScript-code by the browser to show your city correctly. However, the browser won't change it in the link as it's inside quotes there causing the browser to print or use the string as-is.

In other words, the JavaScript-code is not executed in the link.

2
  • I got it, is there any easy way to make it work? Or idk turn the result of the script to a string. Thanks Dec 25, 2010 at 22:45
  • You can only solve it in your PHP-file only, if you have the value available there. Otherwise you'd need to add some JavaScript-code that adds the city-code to the URL.
    – sjngm
    Dec 25, 2010 at 22:50
0

Hey Liso, This is because the short-code gets manipulated by the WP plugin and is outputted as a javascript. So, when you access the shortcode via the above method, the actual value being assigned is this javascript.

3
  • Is there a way so that I convert the script to a simple string and then assign it to a value? Or idk any solution at all.. Dec 25, 2010 at 22:44
  • @Liso: you can have the city as a simple string, in javascript. Use javascript to create the link (or to modify the href)
    – Konerak
    Dec 25, 2010 at 22:49
  • @liso.. yeah.. rather than trying to render the city text in php, try to create the href attribute for this link via javascript. It will be easier than to debug in the source of this plugin.
    – Stoic
    Dec 25, 2010 at 22:53
0

Your problem is that the location is not from PHP. All the shortcode does is put in some javascript that goes to get the location and then prints it.

<script language="javascript">document.write(geoip_city());</script>

What you can do is use some other method of finding the location, based on IP address. Something like this: http://ipinfodb.com/ip_location_api.php

Or you can write some javascript that uses the geoip_city() function that your plugin supplies and change the links of the URLs that need to be changed. If the URL that needed to be changed was in a link with class location_link (and you are using jQuery), you could do:

$('.location_link').each(function(i,v) {
    $(v).attr('href', $(v).attr('href') + geoip_city());
});

The link would initially have to be just ?s= for this to work. This code would need to be done after all the page elements have loaded - you can use jQuery's ready function for that: http://api.jquery.com/ready/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.