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