0

I have been trying to use the answer at How to change the href for a hyperlink using jQuery to modify a WordPress permalink, however I am not having much luck and need assistance.

I am trying to modify the WordPress Permalink on one Hyperlink, across all pages on the site.

<a id="language" href="<?php the_permalink() ?>">German</a>

Lets say the permalink is http://www.wordpress-site.com/about-us/

I want to modify it, using jQuery to read http://www.wordpress-site.com/ger/about-us/

What would my jQuery code need to be in order to accomplish this? It needs to work dynamically, across all pages on the site, identifying http://www.wordpress-site.com/whatever/ in the a#language on all pages, and replacing it with http://www.wordpress-site.com/ger/whatever

Community
  • 1
  • 1
Zach Nicodemous
  • 9,097
  • 9
  • 45
  • 68

1 Answers1

1

You could try something like this:

​$(document).ready(function() {
   $('a#language').attr('href', function() {
      return this.href.replace(/^(http:\/\/www\.wordpress-site\.com)(.*)/, "$1/ger$2");
   });
});​​​

I don´t know your setup but is it really necessary to change the URL on the client side? IMHO this kind of URL rewriting is typically a task for the server.

Michael Weiss
  • 514
  • 1
  • 7
  • 15
  • In principle, yes. But I don´t know WordPress well enough to say if there is an easy way to implement this. Maybe there is a plugin which suits your needs. There are some related questions: [Setup multi languages wordpress](http://stackoverflow.com/questions/172138/setup-multi-languages-wordpress), [Wordpress multilanguage plugin](http://stackoverflow.com/questions/1513346/wordpress-multilanguage-plugin) – Michael Weiss Mar 14 '12 at 14:47
  • OK. I'm not really looking for multi language, the above was just an example of the type of thing I want to accomplish - injecting text into a URL/Permalink. WordPress isn't flexible enough to allow that, I know it very well hence why I wanted to do it with jQuery. I tried your demo code above but it wouldn't work. Do you have a jsFiddle for it? – Zach Nicodemous Mar 14 '12 at 15:18