• 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 to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hey @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

    A (Mostly) complete guide to the WordPress rewrite API – By Christopher Davis

    Endpoints: A little secret for URL manipulation in WordPress – By John Beales

    Hope that helps!

    Thread Starter jwgreen1976

    (@jwgreen1976)

    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 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; + “&” + “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.
    • This reply was modified 7 years, 11 months ago by 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.