I would like to get some value that was included in the http request header and display it on my webpage, i.e:

  • I have a webpage, say: 'mywebsite.com'
  • I have a POST request to 'mywebsite.com' with a variable (lets call it var_A) in its header
  • Then I would like to display the value of var_A on 'mywebsite.com', i.e. add it to the HTML

I've found things like: get_the_content() that will return the POST content of the current page. I can add that to my website using the Code Snippets plugin, however, I can't seem to understand how to then display the content values (like var_A) on the webpage itself.

I'm a bit lost on how to connect the PHP stuff to the JavaScript that runs in the frontend?


You can use filter_var or, more directly, filter_input to retrieve data from the REQUEST data.

$var_a = filter_input( INPUT_POST, 'var_a', FILTER_SANITIZE_STRING );

The above code will get var_a out of the $_POST data, and sanitize according to PHP's FILTER_SANITIZE_STRING. You can read the PHP Manual on Types of filters for other data types.

  • Amazing! This works perfectly with a custom PHP shortcode, thank you! – Jack Jhonsen Mar 3 at 9:47

Sounds like you need to hook into get_query_vars and do a little customizing there.

From the docs:

get_query_var() only retrieves public query variables that are recognized by WP_Query. This means that if you create your own custom URLs with their own query variables, get_query_var() will not retrieve them without some further work (see below). Custom Query Vars

In order to be able to add and work with your own custom query vars that you append to URLs (eg: “http://example.com/some_page/?my_var=foo” – for example using add_query_arg()) you need to add them to the public query variables available to WP_Query. These are built up when WP_Query instantiates, but fortunately are passed through a filter ‘query_vars‘ before they are actually used to populate the $query_vars property of WP_Query.

So, to expose your new, custom query variable to WP_Query hook into the ‘query_vars‘ filter, add your query variable to the $vars array that is passed by the filter, and remember to return the array as the output of your filter function.

Hope this helps!

  • This only handles $_GET parameters and has to have a lot of overhead RE hooking into query_vars. The best way to do this that I'm aware of is to use filter_input or filter_var. – phatskat Mar 2 at 22:11
  • Yes, you're right. I think I misunderstood what you were after to begin with. Glad to see you worked it out! – shark Mar 3 at 14:48

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.