In the PHP world, doing something along these lines in an HTML form is perfectly sane and normal:
<input type="text" name="data[name][first]" />
<input type="text" name="data[name][last]" />
<input type="text" name="data[address][street]" />
<input type="text" name="data[address][city]" />
Then, in your $_POST variable, you'll get an array similar to this:
data = array (
'name' => array (
'first' => value,
'last' => value
),
'address' => array (
'street' => value,
'city' => value
)
You can then access them by using those names in the var, i.e. $firstName = $_POST['data']['name']['first'];.
My problem is that this doesn't work at all in Wordpress. The error I get is that the trim() function cannot be used on arrays.
I've traced "why" it does work to the following piece of code found in the parse_query function of the WP_Query object:
$qv['p'] = absint($qv['p']);
$qv['page_id'] = absint($qv['page_id']);
$qv['year'] = absint($qv['year']);
$qv['monthnum'] = absint($qv['monthnum']);
$qv['day'] = absint($qv['day']);
$qv['w'] = absint($qv['w']);
$qv['m'] = absint($qv['m']);
$qv['paged'] = absint($qv['paged']);
$qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
$qv['pagename'] = trim( $qv['pagename'] );
$qv['name'] = trim( $qv['name'] ); /* Throws trim() error on array! */
if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
Can anyone think of a way to properly use arrays for names of fields within WordPress? Seems like such a common practice outside of WP, yet impossible within it? Maybe there's a replacement 'best practices' for form names?
var_dump($_POST)right before the line that throws that exception? Maybe WordPress is doing something to POST data and messes it up.$qv['name']is expected to be a string, passing an array to it is not going to work. Where do you expect the various parts of that array to end up?$qv['name']might be expected to be a string, but I'm expecting thenameelement of the form to be an array, i.e.$_POST['data'](withdataitself being an array)$_POSTand$qv: are you passing$_POSTto a function, or is this something that gets processed on every page? Maybe you just need to pick names for your custom form parameters that don't already have a different meaning in Wordpress?$_POST, but only because that's what I'm expecting and trying to access once the page reloads. There could be something else in the mix here.