I am trying to pass a parameter to a WordPress site using a URL – for instance:
www.fioriapts.com/?ppc=1
will be the URL.
I am intending to write a function in the functions.php file but the mechanics of how to extract a parameter in WordPress is beyond me. How can it be done?
I am finding a lot of examples on how to add a parameter to a URL using the function add_query_arg()
but have found nothing on how to extract a parameter.
Why not just use the WordPress
get_query_var()
function? WordPress Code ReferenceSince get_query_var can only access query parameters available to WP_Query, in order to access a custom query var like ‘ppc’, you will also need to register this query variable within your plugin or
functions.php
by adding an action during initialization:Or by adding a hook to the query_vars filter:
When passing parameters through the URL you’re able to retrieve the values as GET parameters.
Use this:
It is safer to check for the variable first though:
Here’s a bit of reading on GET/POST params you should look at: http://php.net/manual/en/reserved.variables.get.php
EDIT: I see this answer still gets a lot of traffic years after making it. Please read comments attached to this answer, especially input from @emc who details a WordPress function which accomplishes this goal securely.
You can try this function
Here is what is happening in the function
Here three things are happening.
All of this information plus more info on the thinking behind the function can be found on this link https://www.intechgrity.com/correct-way-get-url-parameter-values-wordpress/
In the call back function, use the $request parameter