rzb
rzb

Reputation: 2153

Getting querystring parameter value in wordpress

I'm making a wordpress plugin. I've used add_query_string() inside anchors in order to load content based on what link the user has clicked. Now I need to know the best way to get the parameter value in the current URI.

It's probably a pretty basic and stupid question, but I'm new to programming so I'm sorry if I misinterpret some terms.

This is the code:

        if ( current_user_can('manage_options') ) {
            echo (
                '<div>
                    <ul>
                        <li><a href="'.add_query_arg( 'adminoption', 1 ).'">option 1</a></li>
                        <li><a href="'.add_query_arg( 'adminoption', 2 ).'">option 2</a></li>
                    </ul>
                </div>'
            );

            // if adminoption == 1 load content A
            // if adminoption == 2 load content B

        }

Upvotes: 12

Views: 85076

Answers (4)

David Carrus
David Carrus

Reputation: 182

I think you are asking for get_query_var() function. In your case you should use get_query_var('adminoption'). Hope it helps

Upvotes: 15

Tom&#225;s Cot
Tom&#225;s Cot

Reputation: 1013

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).

If you want to use custom query vars with this function, you need to use the query_vars filter to modify the list of supported query vars, you can read how to do that in the documentation linked above.

Upvotes: 10

Reine Johansson
Reine Johansson

Reputation: 328

get_query_var('adminoption') only works with standard or registered vars. So for non-standard Wordpress vars you would need to register it first in your functions.php file:

function rj_add_query_vars_filter( $vars ){
    $vars[] = "adminoption";
    return $vars;
}
add_filter( 'query_vars', 'rj_add_query_vars_filter' );

get_query_var('adminoption');

Realize the question is old but hope it helps anyone.

Upvotes: 19

Taylored Web Sites
Taylored Web Sites

Reputation: 1027

Raising hidden answer in the comments by David Carrus:

Anyway you may try with the old php $_GET['adminoption'].

Upvotes: 14

Related Questions