Display values of current POST request on page

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x