I'm using the Qtranslate plugin for my site. It's a pretty cool plugin and works great to a degree. I'm having a problem though when users add links to internal content in the WYSIWYG editor in that the link will just be outputted in one language. I would like to insert some php automatically to all links to internal content created in the WYSIWYG editor of my site. I want <?php echo qtrans_getLanguage(); ?> to be added to every internal link.

For example if the following is done in WP:

<a href="/london">London</a>

I want it to be outputted as this:

<a href="/<?php echo qtrans_getLanguage(); ?>/london/">London</a>

This way any links inserted in content using the WYSIWYG editor will automatically forward to the right language.

There's a further complication that means I don't want external links to have the php added. So if a link looks like this: <a href="http://www.google.com/">Google</a>I want it to stay like this.

Any idea how this could be done? I couldn't find any other posts that were addressing such an issue.

1 Answer 1


Inserting PHP is not really the way you want to do this. You would then need additional code to execute the PHP in the post body. It could be complicated.

  1. Replace the links using a filter on the_content. Something like this:

    function replace_urls_wpse_91463($matches) {
      $ret = '<a';
      if (isset($matches[1])) {
        $ret .= $matches[1];
      if (isset($matches[2])) {
        $home = parse_url(get_home_url());
        $url = parse_url($matches[2]);
        if (isset($url['host']) && $home['host'] !== $url['host']) {
          $link = $matches[2];
        if (!isset($url['host']) && isset($url['path'])) {
          $link = get_home_url('',$url['path']);
        $ret .= 'href="'.$link.'"';
      if (isset($matches[3])) {
        $ret .= $matches[3];
      $ret .= '>';
      if (isset($matches[4])) {
        $ret .= $matches[4];
      $ret .= '</a>';
      return $ret;
    function replace_urls_filter_wpse_91463($content){
      $content = preg_replace_callback('/<a([^>]*)href=(?:"|\')([^"\']*)(?:"|\')([^>]*)>([^<]*)/','replace_urls_wpse_91463',$content);
      return $content;

    And I stress something like that, because that was put together in a hurry and will probably break in all kinds of horrible ways. And besides, parsing HTML with regex is pretty dicey even on a good day. Fun? Yes. Reliable? Not so much. I would recommend option #2. I am sure that could be cleaned up-- a lot-- but again, I would recommend option #2

  2. Create a shortcode. Your people enter [url link="/london"]London[/url]. Something like this:

    function makeurl_sc_wpse_91463($atts,$content) {
        $home = parse_url(get_home_url());
        if (!isset($atts['link'])) return;
        $link = parse_url($atts['link']);
        if (isset($link['host']) && $link['host'] !== $home['host']) {
          $link = $atts['link'];
        } elseif (isset($link['path'])) {
          $link = get_home_url('',$link['path']);
        $pat = '<a href="%s">%s</a>';
      return sprintf($pat,$link,$content);
    add_shortcode('url', 'makeurl_sc_wpse_91463');

    Much nicer, and likely far more reliable. As before, this was put together in a hurry but it does have the minimal functionality mentioned in your question. I wouldn't put it into production without thorough testing and debugging.

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.