Variable by URL on all page links

Asked

Viewed 213 times

2

I am using the following code to pass variables through the URL, in wordpress.

if (isset($_GET['layout'])) {
    $layout = $_GET['layout'];
} else {
    $layout = '1';
}

With $layout I recover the value in the code, where I use to change the layout style.

By calling this url everything works very well. localhost/wp/? layout=2

In this url there are several links to other posts, pages, categories, etc. I would like that when calling a variable in the URL, the same applies to all links on that page.

For example: i start calling the variable layout at the url thus: localhost/wp/? layout=2

And on this page you will have a link to the "about" page: thus: localhost/wp/about

I wanted the variable layout be automatically placed on it and all other links.

That way: localhost/wp/about/? layout=2, localhost/wp/other-page/? layout=2, localhost/wp/category/technology/? layout=2

1 answer

1


With PHP I find it difficult to do this. But with Javascript you can change all links.

The code below checks if the variable layout exists in the URL. If it exists, add it and its value to all links <a> page:

var url_ = new URL(location.href),
    var_ = url_.searchParams.get("layout");
if(var_){
   var a_ = document.body.querySelectorAll("a");
   for(var x=0; x<a_.length; x++){
      a_[x].href += (a_[x].href.indexOf("?") == -1 ? "?" : "&")+"layout="+var_;
   }
}

As the method searchParams no support in IE, follows an alternative form:

var url_ = location.href,
    param = url_.indexOf("layout");
if(param != -1){
   var var_ = url_.substring(param+7,url_.length).match(/^(\d|\w){1,}/)[0],
       a_ = document.body.querySelectorAll("a");
   for(var x=0; x<a_.length; x++){
      a_[x].href += (a_[x].href.indexOf("?") == -1 ? "?" : "&")+"layout="+var_;
   }
}

Edit

To check multiple variables and apply to links, you can insert in an array the names you want to manipulate:

var url_ = location.href,
    param = url_.substring(url_.lastIndexOf("/"), url_.length),
    params = ['layout','teste']; // insira aqui os nomes das variáveis
for(var y=0; y<params.length; y++){
   if(param.indexOf(params[y]) != -1){
      var var_ = url_.substring(url_.indexOf(params[y])+params[y].length+1,url_.length).match(/^(\d|\w){1,}/)[0],
          a_ = document.body.querySelectorAll("a");
      for(var x=0; x<a_.length; x++){
         a_[x].href += (a_[x].href.indexOf("?") == -1 ? "?" : "&")+params[y]+"="+var_;
      }
   }
}
  • worked, now, the variable "layout" is not the only one that I intend to use: for example this code var_ = url_.searchParams.get("layout"); to use another variable I would have to duplicate it.

  • It would be possible to do something like this url_.searchParams.get("layout", "outra_variavel", "outra");

  • I don’t think so. I’m giving another answer because I realized that the searchParams does not work in IE.

  • rsrs, I imagine, blza then.

  • works, you could use multiple vars with your last code?

  • I added a solution to the answer.

  • An elegant and professional solution. I have just one more thing, alias, about a point you raised. (recalling that the values of the variables must be numbers according to the /^(\d){1,}/ ): i was already trying here to use words... /?layout=boxed&skin=red is it possible to change this? leave both numbers and words?

  • This solution is awesome. It even works on IE8.

  • a_ = document.body.querySelectorAll("a"); There are some links that I don’t want to take the variable. It is because there are some external links from the gallery, the footer, social links, etc. Links from posts, pages, categories are the ones that should have this effect. I thought about it: #menu ul li a, #list-posts a

  • a_ = document.body.querySelectorAll(".my-post a, nav a, .menu-top a, .sidebar a"); works perfectly. Thank you

  • You can use the selector to take all the links you want.

  • I put another related question: https://answall.com/q/277851/95735

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.