Avatar of Neil Thompson
Neil Thompson
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Wordpress pass multiple (changeable) parameters to a page and use them in that page

I'm currently working on a WordPress website where I want to make 1 custom landing page but pass 3 parameters in the URL and then use those parameters in the page itself.

Example
http://website.co.uk/landing/daewoo/boilers/in/kent
http://website.co.uk/landing/bosch/airconditioning/in/london

broken into params that is
http://website.co.uk/{customPage}/{manufacturer}/{product}/in/{location}


The content will be the same on every page that is shown but I then need to use the 3 parameters passed in the page.

For example I want to make the title something like "Daewoo Boilers in Kent" and again use those parameters in the page itself like this:

Hadene are one of the largest independent commercial {product} specialists in {location}. We are able to install, service and maintain a wide variety of {manufacturer} {product}. We work with all fuel types, be it gas, LPG, oil or electric. You can count on Hadene in {location} to work with you to create a solution to suit your needs. Our servicing and maintenance covers planned, preventative and remedial work, and our service contracts will cover emergency repairs, breakdown cover and emergency call outs. No {manufacturer} {product} is too big or too small for us to look after for you.


We cover a variety of {location} commercial establishments;

  • Schools, colleges and universities
  • Pubs, bars, nightclubs, hotels and restaurants
  • Council offices, fire stations and police stations
  • Swimming pools, leisure centres and gyms
  • Office blocks and commercial premises
  • Garden centres, shops, retail stores and car dealerships
  • Church buildings
  • Hospitals and care homes

For all your {manufacturer} {product} needs in {location} get a quote now




I'm competent in PHP and have full server access but I'm just not sure how to do this in WordPress and have never made a custom page. I'm not even sure if I simply need a plugin to be able to do this?


I'm a quick learner but need examples to learn by please, not just pointers as I don't understand that way :(


To then get this to be picked up by the search engines I intend to either make the links in a page to be spidered or custom add them to the robots file

Thanks, Neil

WordPressPHP

Avatar of undefined
Last Comment
Neil Thompson

8/22/2022 - Mon
Scott Fell

One option would be to capture the url you are on, Then make an array out of the url where you are splitting on the slashes.

In your functions.php page, create  shortcode https://codex.wordpress.org/Shortcode_API
function foobar_func( $atts ){
   return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );

Open in new window

If you put the shortcode [foobar] on your page, where you place that shortcode will output, "foo and bar".

Now try using the code below. You should see that if you are on page https://yourdomain.com/testing, the return is "testing"

function foobar_func( $atts ){

   global $wp;
   $route = add_query_arg( array(), $wp->request );
   return $route;

}
add_shortcode( 'foobar', 'foobar_func' );

Open in new window


You can see if you are on the top level, and the page is, 'test" that the output will be, 'test"   If you are on https://yourdomain.com/testing/test, the output will be "testing/test".

Can you see from there you can explode that https://www.php.net/manual/en/function.explode.php and end up with an array?

Using your example, http://website.co.uk/{customPage}/{manufacturer}/{product}/in/{location} 

array[0] would be customPage 
array[1] would be manufacturer 
array[2] would be product 
array[3] would be in
array[4] would be location 

You can add on to the output example using the data. Make sure to account for missing data though.
Neil Thompson

ASKER
Many thanks Scott, ill give that a try when I'm back in the office tomorrow

One quick question, will this affect all the normal pages on the website, for example those currently used such as
/about
/contact-us
/services


I ideally only want this to work on the /landing prefixed URL unless the others simply ignore it and work as normal using the PostName structure?
ASKER CERTIFIED SOLUTION
Scott Fell

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Scott Fell

By the way, I had a similar question a few years ago and this helped me a lot.

https://www.experts-exchange.com/questions/29098468/Adding-Multiple-Custom-urls-in-Wordpress.html#a42557556 



All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Neil Thompson

ASKER
Thanks Scott

Yes, I want to have my site working as a normal general wordpress site but create a page that is dynamic for all of the brand, location and items for the search engines to spider and for people to hit. I guess that part is done by simply copying the page and creating a template and assigning the landing page to that

so the same page will show for anything hitting /landing for example but the /landing/daewoo/airconditioning/london would use the daewoo, airconditioning and london in the title and content in the page. All the content will be the same for the /location page apart from what is passed through in the URL
Neil Thompson

ASKER
Ok, I think I'm getting there with your code above but when I add anything after the /landing part of the URL it just goes to the 404 page

https://beta.hadene.co.uk/landing/daewoo/boilers/in/kent/

This is what I've done so far which works:
created a new page in the template file from page.php called page-landing.php
added a basic use of that into the page using "foobar is here: [foobar]"

What I need to be able to do now is use a URL like this https://beta.hadene.co.uk/landing/daewoo/boilers/in/kent/ and for it to go to the /landing page template like it does if I just use https://beta.hadene.co.uk/landing/ at present it just goes to the 404 when I add the additional URL parts after /landing 

not sure if I need to alter anything in my permalink structure of WordPress itself for this to work but also allow the rest of the site to work as normal?
Neil Thompson

ASKER
This is my code in the functions.php page

function landingMainBody_func( $atts ){
   
   global $wp;
   $route = add_query_arg( array(), $wp->request );
   
   $manufacturer  = "## NO MANUFACTURER ##";
   $product = "## NO PRODUCT ##";
   $location = "## NO LOCATION ##";
   
   $result = explode("/", $route);
   
   echo"<br/>";
   echo "## route:".$route."<br/>";
   echo "## result[0]:".$result[0]."<br/>";
   echo "## result[1]:".$result[1]."<br/>";
   echo "## result[2]:".$result[2]."<br/>";
   echo "## result[3]:".$result[3]."<br/>";
   echo "## result[4]:".$result[4]."<br/>";


   if ($result['1'] && $result['2'] && $result['4']){
      $manufacturer  = $result[1];
      $product = $result[2];
      $location = $result[4];
   }


   $result = <<<END
   Hadene are one of the largest independent commercial $product specialists in $location. We are able to install, service and maintain a wide variety of $manufacturer $product. We work with all fuel types, be it gas, LPG, oil or electric. You can count on Hadene in $location to work with you to create a solution to suit your needs. Our servicing and maintenance covers planned, preventative and remedial work, and our service contracts will cover emergency repairs, breakdown cover and emergency call outs. No $manufacturer $product is too big or too small for us to look after for you
      
   We cover a variety of {location} commercial establishments;
   <ul>
      <li>Schools, colleges and universities</li>
      <li>Pubs, bars, nightclubs, hotels and restaurants</li>
      <li>Council offices, fire stations and police stations</li>
      <li>Swimming pools, leisure centres and gyms</li>
      <li>Office blocks and commercial premises</li>
      <li>Garden centres, shops, retail stores and car dealerships</li>
      <li>Church buildings</li>
      <li>Hospitals and care homes</li>
   </ul>
   For all your $manufacturer $product needs in $location get a quote now


   END;
   
   
   return $result;


}
add_shortcode( 'landingMainBody', 'landingMainBody_func' );

Open in new window


which is great and called with [landingMainBody] so just  need to be able to pass the parameters in the URL now without the 404 page
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Scott Fell

It is 404 because that page does not exist. You have to physically make the page in Wordpress by going to Pages -> Add New. Then your first page would be called, "landing".  Then another page called daewoo where you make "landing" the parent page and that will give you the route /landing/daewoo.  With a lot of combinations, that can be a lot to do manually.

If you look at my old question, https://www.experts-exchange.com/questions/29098468/Adding-Multiple-Custom-urls-in-Wordpress.html#a42557556  I think this is what you want where you can generate the routes on the fly.  I will take a look at that again myself later. 

Neil Thompson

ASKER
Thanks Scott, yes the 2nd one is correct, I want to be able to do them all on the fly so only have 1 dynamic "not normal" page (/landing) and if the URL matches that then also take all the extra part of the URL

Thanks so much for the help.
Neil Thompson

ASKER
I've got a little further. I added this to the functions.php page

function add_query_vars($aVars) {
   $aVars[] = "h_manufacturer,h_product,h_in,h_location";
   return $aVars;
}
add_filter('query_vars', 'add_query_vars');


function add_rewrite_rules($aRules) {
   $aNewRules = array('landing/([^/]+)/?$' => 'index.php?pagename=landing&h_manufacturer=$matches[1]&h_product=$matches[2]&h_in=$matches[3]&h_location=$matches[4]');
   $aRules = $aNewRules + $aRules;
   return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');

Open in new window


and it now works for the first parameter (e.g.daewoo) but doesnt work for any more, could this be something in this, im very basic at regex

$aNewRules = array('landing/([^/]+)/?$' => 'index.php?pagename=landing&h_manufacturer=$matches[1]&h_product=$matches[2]&h_in=$matches[3]&h_location=$matches[4]');

This works
https://beta.hadene.co.uk/landing/daewoo/

this fails back to the 404
https://beta.hadene.co.uk/landing/daewoo/heating/

i'm resaving the permalinks after each change as I think this is needed to flush the changes
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Scott Fell

Looks like you are on it.

My RegEx is not very good.  See if this helps https://mixedanalytics.com/blog/regex-match-number-subdirectories-url/
Neil Thompson

ASKER
Great help on this query. This was what I finally finished with which worked if others have the same issue

in functions.php this matches the URL /boilers and takes 2 additional parameters in the URL. If you want more/less parameters add or remove this for each one in the regex below
[^/]+

Open in new window


function add_rewrite_rules($aRules) {
   $aNewRules = array('boilers/([^/]+/[^/]+)/?$' => 'index.php?pagename=boilers');   
   $aRules = $aNewRules + $aRules;
   return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');

Open in new window


again in functions.php and called using [landingBoilerMainBody] in the content page itself explodes the URL checking that part 0 is boilers before sanitizing (not yet done) and tweaking the 2 passed additional parameters such as /boilers/daewoo/london in my case the manufacturer and location

these are then assigned to variables and used in the text which is returned to the page

function landingBoilerMainBody_func(){
   
   global $wp;
   $route = add_query_arg( array(), $wp->request );
   
   $manufacturer     = "";
   $location       = "";
   
   $result = explode("/", $route);


   // match the boilers page or redirect to home
   if ($result['0']  == 'boilers'){         
      
      if ($result['1'] && $result['2'] ){
         
         // sanitise and populate results
         $manufacturer  = ucfirst($result[1]);
         $location = ucfirst($result[2]);
         
      } else {
         
         // redirect to home
         //wp_redirect(index.php);
         
      }
      
   }


   $result = <<<END
   <h1>For all your $manufacturer boiler needs in $location get a quote now</h1>
   We are one of the largest independent commercial boiler specialists in $location. We are able to install, service and maintain a wide variety of $manufacturer boilers. We work with all fuel types, be it gas, LPG, oil or electric. You can count on us in $location to work with you to create a solution to suit your needs. Our servicing and maintenance covers planned, preventative and remedial work, and our service contracts will cover emergency repairs, breakdown cover and emergency call outs. No $manufacturer boiler is too big or too small for us to look after for you
   <br/><br/>
   For all your $manufacturer boiler needs in $location get a quote now
   END;   
   
   return $result;


}
add_shortcode( 'landingBoilerMainBody', 'landingBoilerMainBody_func' );

Open in new window




Scott Fell

That's awesome!

So you don't cloud up your functions.php, you can create another php file that you can include in your functions.php to manage all of this.


⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Neil Thompson

ASKER
Thanks Scott, really appreciate the help. I need to add more like above so will move them to a separate file, another great idea :)