Add Suffix To All Internal URLs
-
21-08-2019 - |
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>
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:
- Obtain all the elements ( getElementsByTagName('a') )
- Run a foreach loop on them
- 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