0

In my WP site-- eg. http://www.mydomain.com-- a request has to be passed to a custom URL-- http://www.mydomain.com/custompage?param1=abc&param2=def

Now, I wish to obtain the parameters passed to that URL from a function in functions.php of that theme. This function is executed when the above (example) URL is loaded...

How do I do this? For above eg. I wish to obtain first parameter=abc and second parameter=def-- I already know that the parameters are named param1 and param2.

1
  • Tell us more about the 'custompage'. Like: What is the page id? Is the page already using a page template? Jun 5, 2013 at 13:06

2 Answers 2

2

First, you register your query vars param1 and param2:

function wpse_101951_query_vars( $qv ) {
    $qv[] = 'param1';
    $qv[] = 'param2';
    return $qv;
}
add_filter( 'query_vars', 'wpse_101951_query_vars' );

To use this information, you can pretty much hook into any action or filter after parse_query. That's the first action available after the query vars are set, so it's the first action where you can use get_query_var. Here's an example:

function wpse_101951_get_params() {
    if ( $param1 = get_query_var( 'param1' ) ) {
        # Do something as a result of param1 being set
    }
    if ( $param2 = get_query_var( 'param2' ) ) {
        # Do something as a result of param2 being set
    }
}
add_action( 'parse_query', 'wpse_101951_get_params' );
0

From my limited experience I would shy away from polluting WordPress's query variables without good reason. These are used when making a database query and are quite easy to mess up.

If you are not intent on changing WP's main query object, just use PHP's $_GET or $_REQUEST.

function get_url_params() {
    if ( isset($_GET['param1']) )
        $param1 = $_GET['param1'];

    if ( isset($_GET['param2']) )
        $param2 = $_GET['param2'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.