Sometimes you may have a URL that has parameters or variables included in it that you wish to display on a page or post within WordPress. In cases like this all that’s needed is a simple function to get the information from the URL onto the page itself.
Let’s take the following URL as an example:
https://www.example.com/?firstname=Warren&lastname=Chandler
Simply paste the following into your theme or child theme’s functions.php file.
function ShowURLVars( $atts ) { extract( shortcode_atts( array( 'param' => 'param', ), $atts ) ); return stripslashes(esc_attr(esc_html($_GET[$param]))); } add_shortcode('URLVar', 'ShowURLVars');
Then once you’re done, add the shortcode to the page or post itself like so.
Hey there, [URLVar param='firstname'] [URLVar param='lastname']. Welcome to my website.
This will output: Hey there, Warren Chandler. Welcome to my website.
Nice and easy!
Hi Warren,
Thank you so much for this, it works great!
I use your code to display brands in the content of my website, taken from the url parameter, for example: ?brand_filter=maxi-cosi
Only problem is: many brands are multiple words and now displayed as “maxi-cosi”.
Is there a way to replace the hypens with a space and transform the first letter to a capital?
Many thanks in advance!
Kind regards,
Peter