0

I am trying to intercept the GET request of a post and add a value to it.

function foo($request) {
    $request['vid'] = wp_generate_uuid4();

    return $request;
}
add_filter( 'request', 'foo' );

and hope that it would be available later with

$_REQUEST['vid']

but no access so far any ideas?

  • WordPress doesn't add vid to the $_REQUEST array. Use get_query_var( 'vid' ) to access the vid value.. – Sally CJ May 15 at 13:10
  • @SallyCJ 10x man it works. Put in an answer I will accept it – Bat Man May 15 at 13:20
  • Done, @BatMan.. – Sally CJ May 15 at 13:40
1

WordPress doesn't add the vid to the $_REQUEST array. Instead, it's saved in a class property — see WP::$query_vars which is an array.

And to access the value of items in that array, use get_query_var() like so in your case:

$vid = get_query_var( 'vid' );
echo "vid value is $vid";
  • Is it possible if 2 requests happen almost simultaneously that the second one overrides the value returned by get_query_var( 'vid' ) if using it in functions.php when serving a post ? – Bat Man May 16 at 13:25
  • I don't really understand your question, but plugins, themes and custom code can change the value at anytime during the same page load/request. E.g. Using set_query_var( 'vid', 'value' ). – Sally CJ May 16 at 17:56
  • Ok Can second page load/request change it for the first being in process for it should trigger the same filter hook? Or they stack 1 after another? – Bat Man May 17 at 11:13
  • 1
    They stack 1 after another - you get a fresh value on each page load because the data is persistent in the same request only. – Sally CJ May 17 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.