12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Oct 1, 2021

How to get the value of query variables that have square brackets

Sridhar Katakam

There could be times when your website pages have query variables like this:

example.com/sample-page/?customer[firstname]=Michael

Ideally square brackets, [ and ] should be percent encoded as %5B and %5D but some systems like payment processors may take customer info and pass them to another page in your site with square brackets in the URL.

So if you wish to extract “Michael” from the example above and output it on the page having that query variable in its URL, here’s the PHP:

<?php
	$firstname = $_GET['customer']['firstname'];

	echo ( isset( $firstname ) ) ? $firstname : '';
?>

If you wish to define a custom function that returns the above, here it is:

/**
 * Returns the value of customer[firstname] URL parameter.
 *
 * Sample usage: custom_get_query_var()
 */
function custom_get_query_var() {	
	$firstname = $_GET['customer']['firstname'];

	return ( isset( $firstname ) ) ? $firstname : 'user';
}

This can be used with for example, PHP Function Return value of Dynamic Data feature in Oxygen to show the URL parameter value alongside other text.

References

https://stackoverflow.com/a/39294315/778809

https://stackoverflow.com/a/44901804/778809

https://stackoverflow.com/a/40106462/778809

https://wordpress.org/support/topic/_get-in-wordpress/

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right