7

I need to generate a simple error message on a page by passing a variable through the URL.

The URL is structured as follows:

http://site.com/parent-category/category/?error=pause

I'm sure it's the permalinks rewrite interfering, but I'm not sure how to resolve it.

1
  • You need to know you to generate using $_GET variable, or how to add query args in url? Commented Feb 6, 2012 at 17:18

2 Answers 2

13

Try adding the variable to the WordPress' array of 'recognised query variables'...

add_filter('query_vars', 'my_register_query_vars' );
function my_register_query_vars( $qvars ){
    //Add query variable to $qvars array
    $qvars[] = 'my_error';
    return $qvars;
}

Then the value of 'my_error' can be found via get_query_var('my_error'). (See Codex)

EDIT

From Otto's comment, it's better to do:

add_action('init','add_my_error');
function add_my_error() { 
    global $wp; 
    $wp->add_query_var('my_error'); 
}
3
  • I wouldn't use that filter in particular. We have functions that you should use for that instead. add_action('init','add_my_error'); function add_my_error() { global $wp; $wp->add_query_var('my_error'); } Commented Feb 6, 2012 at 20:49
  • 1
    Cool. It doesn't really matter, actually, I just prefer to not use deep hooks when shallow ones will do the job. :) Commented Feb 6, 2012 at 21:01
  • I always use add_rewrite_tag($tagname,$regex). Any difference? Commented Feb 6, 2012 at 21:10
0

This was the only way I could get this to work

add_action('init','add_query_args');
function add_query_args()
{ 
    add_query_arg( 'error', 'pause' );
}

http://codex.wordpress.org/Function_Reference/add_query_arg

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.