How to parse a custom url (within WP site) and obtain params passed to that URL

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.

Replay

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

Category: urls Time: 2013-06-05 Views: 1
Tags: urls parameter

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.180 (s). 12 q(s)