2

I am creating a plugin that resides at http://localhost/test/wp-admin/options-general.php?page=my-plugin

I am trying to add a query string to this page so that it can be used in my plugin such as http://localhost/test/wp-admin/options-general.php?page=my-plugin?myVar=cool

The problem is that this prompts wordpress to display the "You do not have sufficient permissions to access this page." page.

How can I add a query string to my plugin URL? Is this documented someplace?

thanks for the help.

1
  • Nevermind, I just took another look at the url and it looks like the query string has already been started so the answer is localhost/test/wp-admin/… WHOOPS!
    – mike
    Commented Apr 15, 2011 at 14:51

1 Answer 1

8

When you don't know if query string was started or not you can use add_query_arg which it knows how to deal with that and adds the "?" or "&" marks (which ever one is needed) to the query string.

Update

By popular demand I'm adding a few examples that are from the codex:

Using get_permalink:

Since get_permalink() returns a full URL, you could use that when you want to add variables to a post's page.

//  This would output whatever the URL to post ID 9 is, with 'hello=there' appended with either ? or &, depending on what's needed
echo add_query_arg( 'hello', 'there', get_permalink(9) );

more general:

Assuming we're at the WordPress URL "http://blog.example.com/client/?s=word"...

//  This would output '/client/?s=word&foo=bar'
echo add_query_arg( 'foo', 'bar' );

//  This would output '/client/?s=word&foo=bar&baz=tiny'
$arr_params = array ( 'foo' => 'bar', 'baz' => 'tiny' );
echo add_query_arg( $arr_params );

Or for use with any link you have you can pass the link uri:

//say your link is: http://wordpress.stackexchange.com/questions/14827/
//then use:

echo add_query_arg( 'hello', 'world','http://wordpress.stackexchange.com/questions/14827/');

to get http://wordpress.stackexchange.com/questions/14827/?hello=world

Example plugin page URL with extra query args:

$query_args = array( 'page' => 'your-plugin-page', 'foo' => 'bar' );
echo add_query_arg( $query_args, admin_url( '/options-general.php' ) )

// outputs
// http://example.com/wp-admin/options-general.php?page=your-plugin-page&foo=bar
3
  • 1
    Nice. Beat me to it. Want to add an example of how to use the function? It might help to see it in action, since there are several valid ways of using it. Commented Apr 15, 2011 at 15:12
  • I added a few examples, but feel free to edit and add more.
    – Bainternet
    Commented Apr 15, 2011 at 16:18
  • 1
    I've added another.. ;)
    – t31os
    Commented Apr 15, 2011 at 16:49

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.