I just started to learn PHP and doing things with WordPress and I am a bit confused about information in Codex. Was told that Codex got all info I required but I got stuck with it.
What array fields are in $_REQUEST
in WordPress? Can not find it in Codex.
E.g.: people use
$my_contact = $_REQUEST['contact'];
How do they know $_REQUEST
has 'contact'
field?
Is there any workflow to find informations about variables which are not described in Codex? Should I print off all the array to see what fields are inside?
Google does not help me.. about $_REQUEST.
This is mostly pure PHP, but it does have WordPress twist.
PHP has number of superglobal variables, that contain information relevant to current request. Out of those:
$_GET
contains info from URL (HTTP GET request)$_POST
info from form submission (HTTP POST request)$_COOKIES
about cookies set$_REQUEST
is combination of the above (according to docs$_COOKIES
can be commonly configured to skip for better security)However WP enforces its own logic – during load process
wp_magic_quotes()
processes variables to emulate magic quotes setting and enforces$_REQUEST
to contain combination of$_GET
and$_POST
, no matter what PHP configuration says.So in WordPress environment it will contain GET and/or POST request data. What data exactly that is will depend entirely which page you are on and what is happening on it.
Just a note about
$_REQUEST
: Whenever you see this in a code you know it is written by a beginner. As @Rarst explained it is a combination of multiple sources. But why should anyone want to process data which should be send per POST only if they are in fact send per GET?Do not accept data from an input stream you havenât declared previously. Use
$_GET
if you want GET and$_POST
if you want POST. Nothing else.To access POSTed data without WordPressâ intervention use the input stream wrapper
php://input
.So, instead of â¦
⦠use â¦
And donât forget data validation.
You mentioned printing off the array, so you might know how to do this already. You can see all the elements of an array in PHP by executing
print_r($_REQUEST);
. This would give you the exact information each page has access to from $_REQUEST.Although the caveat here is that every page might have different keys set. Also, it might be beneficial to write this to a temporary logfile, depending on if you are in production. You wouldn’t want your visitors seeing this output.
I needed it just for the test. Did as you advised, wrote all fiels into a file, used hook ‘comment_post’ to make sure $_REQUEST has data just after a comment is posted.
To answer this following question of yours:
My answer is this :
They know because They have written this hidden input field inside the
<form>
(as the first input field) like bellow:They can also enable their custom search template instead of the default search.php by writing the following code in functions.php
Hope it helps you to understand the fact how do they know what field
$_REQUEST
has.