12

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

        }

4 Answers 4

19

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.

Sign up to request clarification or add additional context in comments.

1 Comment

What are you adding "group" to the $vars[] array? Shouldn't it be $vars[] = "adminoption";?
15

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

7 Comments

Thanks David. Nice suggestion, but that returned blank for me. Truth is I don't even know if I should be using add_query_arg(). All I need to do is to show content on the same page depending on query string. I could just create one page for each link but that's not an option as I'd not be learning anything, and learning is the main reason I'm messing with creating a plugin.
What is your url like after you click on a link? Is it something like www.yoursite.com/?adminoption=1 Just to know if your problem starts on add_query_arg(). Anyway you may try with the old php $_GET['adminoption'].
Yes, the URL after clicking on a link is fine. And $_GET['adminoption'] worked like a charm. Thanks a bunch! Do I have to worry about security while using $_GET though?
I suppose you are only showing a content based on the argument and not putting the value inside a DB query, nor using it on page directly, so you shouldn't have any problem. I suppose Wp uses a function to retrieve values, for security reasons, adding controls and sanitizing them but this shouldn't be the case. Strange that the WP function doesn't work. Did you get any error? or just a blank value? What kind of page is the one where you are writing the code?
Just plain blank value. Could it be the scope I'm calling it from? The code is inside a static function.
|
14

Raising hidden answer in the comments by David Carrus:

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

Comments

10

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.

1 Comment

+1 surprisingly get_query_var('key') doesn't work with custom arguments and this is the only answer that mentions it

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.