Title: Using Javascript to append URL parameter not working
Last modified: June 12, 2018

---

# Using Javascript to append URL parameter not working

 *  [jwgreen1976](https://wordpress.org/support/users/jwgreen1976/)
 * (@jwgreen1976)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/using-javascript-to-append-url-parameter-not-working/)
 * Hi, I have a bit of javascript that retrieves url parameters when someone arrives
   at my site, and then stores those parameters in LocalStorage. It then retrieves
   one of the parameters and appends a code to certain classes of URLs on the page.
 * It works great on our existing site. However, we’re converting to WordPress, 
   and WordPress is not allowing the javascript to append the parameter to my urls.
 * It does successfully create the LocalStorage item. WordPress just won’t let the
   javascript append the url parameter to my links. Is there anything special I 
   need to do to have WordPress allow this?
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fusing-javascript-to-append-url-parameter-not-working%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [juan2machado](https://wordpress.org/support/users/juan2machado/)
 * (@juan2machado)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/using-javascript-to-append-url-parameter-not-working/#post-10392590)
 * Hey [@jwgreen1976](https://wordpress.org/support/users/jwgreen1976/).
 * As you describe it, it looks like you need to add custom URL endpoints in WordPress
   in order for WordPress to differentiate your custom parameters from system permalinks.
 * I came across with this issue while on development of a plugin that needs to 
   catch some parameters from URLs too.
 * Here are a few resources that helped me a lot:
 * [The WordPress Rewrite API](http://codex.wordpress.org/Rewrite_API)
 * [A (Mostly) complete guide to the WordPress rewrite API – By Christopher Davis](https://www.pmg.com/blog/a-mostly-complete-guide-to-the-wordpress-rewrite-api/)
 * [Endpoints: A little secret for URL manipulation in WordPress – By John Beales](https://johnbeales.com/2009/endpoints-a-little-secret-for-url-manipulation-in-wordpress/)
 * Hope that helps!
 *  Thread Starter [jwgreen1976](https://wordpress.org/support/users/jwgreen1976/)
 * (@jwgreen1976)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/using-javascript-to-append-url-parameter-not-working/#post-10392684)
 * Thank you!
 * I did add the following to the theme’s functions.php file, with “mc” being the
   parameter key I want recognized:
 * function add_query_vars_filter( $vars)
    { $vars[]=”mc”; return $vars; } add_filter(‘
   query_vars’, ‘add_query_vars_filter’ );
 * I realize there may be a way to do this all in php, but the parameter won’t persist
   if the visitor starts a new session, which is why I’m sticking the parameter 
   in localStorage with Javascript.
 * The localStorage object is being created successfully, and when I open the console,
   the link that I want changed has an href property which does contains the parameter:
   
   h ttps://xxxxxxxxxxxx/Account/Login?code=xxxxxxx&mc=6540
 * however, the innerHTML is not successfully having the parameter appended:
    a 
   href=”[https://xxxxxxxxxxxx/Account/Register?code=xxxxxxx”>Apply](https://xxxxxxxxxxxx/Account/Register?code=xxxxxxx”>Apply)
   Now</a
 * I will take a look at the links you gave to see if I’m missing anything. Like
   I said, on our straight HTML, CSS and Javascript site, this works perfectly:
 * function getAllUrlParams() {
    //assign URL parameters to variable console.log(“
   URL Params loaded”); var queryString = window.location.search.substring(1);
 *  if (queryString) {
    //split queryString into pairs var pairs= queryString.split(‘&’);
   console.log(pairs);
 * //split pairs into object
    var result = {}; pairs.forEach(function(pair) { pair
   = pair.split(‘=’); result[pair[0]] = decodeURIComponent(pair[1] || ”); });
 * //check localStorage to prevent overwriting values, write values if empty
    if(
   localStorage.getItem(‘brazosMC’) == null) { localStorage.setItem(‘brazosMC’, 
   JSON.stringify(result)); } }
 *  var elements = document.getElementsByClassName(“apply”);
    var i = elements.length;
   console.log(elements); var params = JSON.parse(localStorage.getItem(‘brazosMC’));
   console.log(params);
 *  if (params !== null) {
    if (“mc” in params) { var url = “[https://xxxxxxxxxx/Account/Login?code=xxxxxxx&#8221](https://xxxxxxxxxx/Account/Login?code=xxxxxxx&#8221);
   + “&” + “mc=” + params.mc; while (i–) { elements[i].href = url; } } else { console.
   log(“Key undefined”); } } }
    -  This reply was modified 7 years, 11 months ago by [jwgreen1976](https://wordpress.org/support/users/jwgreen1976/).
    -  This reply was modified 7 years, 11 months ago by [jwgreen1976](https://wordpress.org/support/users/jwgreen1976/).

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Using Javascript to append URL parameter not working’ is closed to new
replies.

## Tags

 * [custom endpoints](https://wordpress.org/support/topic-tag/custom-endpoints/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [jwgreen1976](https://wordpress.org/support/users/jwgreen1976/)
 * Last activity: [7 years, 11 months ago](https://wordpress.org/support/topic/using-javascript-to-append-url-parameter-not-working/#post-10392684)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
