Adding Custom Query String URL Variables in WordPress and How to Retrieve Them

One flaw with WordPress is when you use their permalink structure, PHP can no longer recognize functions like $_GET[‘variable_name’] or $_SERVER[‘QUERY_STRING’].  I am not dissing the permalink structure, it is very important as far as SEO and usability are concerned, but from a programming perspective, it can create a hurdle when it comes to passing variables via the URL.

The solution to get the current page’s URL parameters without a plugin

Here is an example.  Let’s say you have a custom page template, and you want to access a URL query string parameter.

Here is the example URL with custom query string variables:

The 2 URL variables in the query string are var1 and var2.  With the values of “I am awesome” and “PHP Rocks.”  Now, you want to access these query string variables.  Here is the code:

$queryURL = parse_url( html_entity_decode( esc_url( add_query_arg( $arr_params ) ) ) );
parse_str( $queryURL['query'], $getVar );
$variableNum1 = $getVar['var1'];
$variableNum2 = $getVar['var2'];
echo 'Describe yourself: ' . $variableNum1 . '. What do you like most about PHP? ' . $variableNum2;

This code would output:
Describe yourself: I am awesome. What do you like most about PHP? PHP Rocks

Where can you use this code?

Anywhere inside of your WordPress website where PHP is accepted. Like in your theme files: “header.php,” “page.php,” etc.

Now, I can develop custom functionality inside of my WordPress website without learning WordPress development too in-depth.