Use JS in WordPress to append query params to urls without resetting on page click

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 =;
    $(".add-query-params").attr("href", queryString);

Example link in code:


<h1>1. Stuff! </h1>

words!<a class="add-query-params" href="">Page 1</a></span> 

<h1>2. Things! </h1>

words!<a class="add-query-params"  href="">Page 2</a></span> 

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *