Question

I need to add a suffix of ?hl=foo to the end of all internal URLs on my site.

Im not sure of the best way to do this because of complications such as...

<a href="http://www.example.com">My Site</a>
<a target="_blank" href="http://www.example.com">My Site</a>
<a class="a-class" href="http://www.example.com">My Site</a>
Was it helpful?

Solution

PHP

Just create a variable that you echo in the source.

Example:

<?php $my_get = "?hl=foo"; ?>

<a href="http://mysite.com<?=$my_get?>"> Yadda </a>

Using Javascript to change each a's href attribute:

  1. Obtain all the elements ( getElementsByTagName('a') )
  2. Run a foreach loop on them
  3. Within the foreach, concat the existing href (http://mysite.com) and ?hl=f00

OTHER TIPS

Try the output_add_rewrite_var function:

<?php
    ob_start();
    output_add_rewrite_var('hl', 'foo');
?>
<a href="/">My Site</a>
<a target="_blank" href="/">My Site</a>
<a class="a-class" href="/">My Site</a>

But I don’t think it works with absolute URLs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top