WordPress is very foreign to me. I have a site with clickable (tracking) links, and would like to append the query parameters that come through to the page to the end of the URL, and stay in the browser after the user clicks to next page. (In the end these params get passed back to my tracking system, I use them to optimize my page.)

Currently, the params disappear when user clicks “next”. I would like the params to carry from page to page, regardless of what the user clicks.

This is the current script in the Tracking Code on WP (enabled to run on all pages, after )

<script type="text/javascript">
$(function() {
    var queryString = location.search;
    $(".add-query-params").attr("href", queryString);
});
</script>

Example link in code:

<!--nextpage-->

<h1>1. Stuff! </h1>

words!<a class="add-query-params" href="http://track.link.com/example/1">Page 1</a></span> 

<!--nextpage-->
<h1>2. Things! </h1>

words!<a class="add-query-params"  href="http://track.link.com/example/2">Page 2</a></span> 

Ideally, the user would populate the URL with the parameters, and it would look something like this:

http://track.link.com/example/2?utm_source=sourceplace&utm_content=contentwords&utm_term=yougettheidea

However WP removes all the params each time the user clicks “Next”, or another article on the page.

I’ve looked at many plugins but none looked like they solved this problem. Same issues occur with PHP (so let’s stick with JavaScript please).

I considered saving the url to a variable, and appending the query params, and saving it as a new custom variable, however I didn’t know how to do that/if it would work.

This is my first SO question, so please forgive me if you hate it! Suggestions for plugins are also appreciated! Thanks everyone :)!

Read more here: Use JS in WordPress to append query params to urls without resetting on page click


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: