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!
Leave A Comment