_get parameter from url wordpress

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. 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. Thanks in advance for any help.

_get parameter from url wordpress

Marc

4,4393 gold badges40 silver badges59 bronze badges

asked Nov 30, 2012 at 20:23

Why not just use the WordPress get_query_var() function? WordPress Code Reference

// 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 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:

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' );

answered Feb 22, 2018 at 15:12

MarcMarc

4,4393 gold badges40 silver badges59 bronze badges

6

When passing parameters through the URL you're able to retrieve the values as GET parameters.

Use this:

$variable = $_GET['param_name'];

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

It is safer to check for the variable first though:

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

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.

answered Nov 30, 2012 at 20:34

GrambotGrambot

4,2555 gold badges29 silver badges43 bronze badges

8

You can try this function

/**
 * 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 is what is happening in the function

Here three things are happening.

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

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/

answered Feb 20, 2018 at 20:55

_get parameter from url wordpress

briankipbriankip

2,4062 gold badges22 silver badges26 bronze badges

In the call back function, use the $request parameter

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

_get parameter from url wordpress

Suraj Rao

29.1k11 gold badges95 silver badges100 bronze badges

answered May 13, 2019 at 9:49

MohanMohan

3351 gold badge3 silver badges11 bronze badges

How do I find the parameter of a URL?

Method 1: Using the URLSearchParams Object The URLSearchParams is an interface used to provide methods that can be used to work with an URL. The URL string is first separated to get only the parameters portion of the URL. The split() method is used on the given URL with the “?” separator.

How do I get query params in WordPress?

To get a vars from the query string you can use PHP's $_GET['key'] method. Depending on what you are doing, you can also use get_query_var('key') , this function works with parameters accepted by the WP_Query class (cat, author, etc).

How do I pass query string in WordPress?

The Process. The first step was to create a function to add variables to WordPress' query string and then hook that function into the query_vars hook. Next, we created a function to add rules to WordPress' existing array of rewrite rules and hook that function into the rewrite_rules_array hook.

How can I get params in PHP?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.