Extract parameter from url in WordPress

I am trying to pass a parameter to a WordPress site using a URL, for example:

www.fioriapts.com/?ppc=1 is the URL.

I am going to write a function in the functions.php file, but the mechanics of how to extract a parameter in WordPress are outside of me. I find many examples of how to add a parameter to a URL using the add_query_arg() function, but found nothing on how to extract the parameter. Thank you in advance for any help.

+22
php wordpress
source share
4 answers

When passing parameters through a URL, you can get the values ​​as GET parameters.

Use this:

 $variable = $_GET['param_name']; //Or as you have it $ppc = $_GET['ppc']; 

It is safer to check the variable first:

 if (isset($_GET['ppc'])) { $ppc = $_GET['ppc']; } else { //Handle the case where there is no parameter } 

Here's a little reading of the GET / POST parameters you should look at: http://php.net/manual/en/reserved.variables.get.php

EDIT: I see that this answer still gets a lot of traffic years after it was created. Please read the comments on this answer, especially the @emc comments, which describe in detail the WordPress function that performs this task safely.

+37
source share

Why not just use get_query_var() in WordPress? Code Link

 // Test if the query exists at the URL if ( get_query_var('ppc') ) { // If so echo the value echo get_query_var('ppc'); } 

Since get_query_var can only access query parameters available for WP_Query, to access a custom var query, such as 'ppc', you also need to register this query variable in your plugin or functions.php by adding an action during initialization:

 add_action('init','add_get_val'); function add_get_val() { global $wp; $wp->add_query_var('ppc'); } 

Or by adding a hook to the query_vars filter:

 function add_query_vars_filter( $vars ){ $vars[] = "ppc"; return $vars; } add_filter( 'query_vars', 'add_query_vars_filter' ); 
+22
source share

You can try this feature.

 /** * Gets the request parameter. * * @param string $key The query parameter * @param string $default The default value to return if not found * * @return string The request parameter. */ function get_request_parameter( $key, $default = '' ) { // If not request set if ( ! isset( $_REQUEST[ $key ] ) || empty( $_REQUEST[ $key ] ) ) { return $default; } // Set so process it return strip_tags( (string) wp_unslash( $_REQUEST[ $key ] ) ); } 

Here's what happens in the function

Three things happen here.

  • First, we check whether the request key is present or not. If not, just return the default value.
  • If it is installed, we first remove the slashes by doing wp_unslash. Read here why it's better than stripslashes_deep.
  • Then we sanitize the value by doing simple strip_tags. If you expect rich text from a parameter, run it through wp_kses or similar functions.

All this information plus additional information about the thinking behind the function can be found at this link https://www.intechgrity.com/correct-way-get-url-parameter-values-wordpress/

+2
source share

In the callback function, use the $ request parameter

 $parameters = $request->get_params(); echo $parameters['ppc']; 
0
source share

All Articles