WordPress Development Asked by kan1 on December 26, 2021
Please help,
I am using a wp site as medium between e.g. a news site lets call it A and the actual site that e.g. sells a particular product lets call it B.
A —- medium site — B
news site —- wp — product site
What I need is to keep 2 url parameters from site A to site B.
A good example that illustrates the above is the news site has an article for NBA on site A
one of the links is an advertisement e.g. for basket shoes.
If a customer clicks it goes to the wp medium site where he can chooses color, style and etc… then if he decides to buy something it is redirected to site B.
What I need and it is crucial is that the wp site passes on the 2 url parameters from the initial link on site A
What I was thinking was for the wp site whenever it sees an “a href” to add the parameters from variable 1 and variable 2 so all the links are having those 2 parameters and pass them on.
Can I use the following script?
function getQueryParams(qs) {
qs = qs.split("+").join(" ");
var params = {}, tokens, re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
} return params;}
var query = getQueryParams(document.location.search);
$('a').each(function () {
var href = $(this).attr('href');
if (href) {
href += (href.match(/?/) ? '&' : '?') + 'affiliate=' + query.affiliate + '&pname=' + query.pname;
$(this).attr('href', href);
}
});
The 1st part makes all the url texts utf8 and not % encoded and the second part should pass on the 2 variables in concern.
Please help where I should place it and how I should call it?
Just in case somebody else has a similar problem I managed to solve it using this plug in
and then I added on the footer script the following code
<script type="text/javascript">
function getQueryParams(qs) {
qs = qs.split("+").join(" ");
var params = {}, tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}
return params;
}
var query = getQueryParams(document.location.search);
jQuery('a').each(function () {
var href = jQuery(this).attr('href');
if (href) {
href += (href.match(/?/) ? '&' : '?') + 'affiliate=' + query.affiliate + '&pname=' + query.pname;
jQuery(this).attr('href', href);
}
});
</script>
Answered by kan1 on December 26, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP