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.

其他提示

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!

许可以下: CC-BY-SA归因
scroll top