0

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?

5
  • 1
    Can you post a var_dump($_POST) right before the line that throws that exception? Maybe WordPress is doing something to POST data and messes it up. Commented Sep 13, 2013 at 22:45
  • If $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? Commented Sep 13, 2013 at 23:43
  • To WordPress, $qv['name'] might be expected to be a string, but I'm expecting the name element of the form to be an array, i.e. $_POST['data'] (with data itself being an array) Commented Sep 13, 2013 at 23:51
  • I'm not clear on the relationship between $_POST and $qv: are you passing $_POST to 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? Commented Sep 14, 2013 at 0:13
  • To be honest, I'm not sure of the relationship either - why sending data through a form within wordpress goes down the 'query' route. I assume it's got something to do with $_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. Commented Sep 14, 2013 at 7:01

1 Answer 1

1

The solution came when I changed the name of the fields, particularly the 'base' of the array.

In the example I provided, it was data, which then had several elements. In this case, data happened to also be a custom post type that I registered with WordPress in my plugin. I don't know exactly what happened or why, but somewhere a long the line it mixed up my array with what was supposed to be queried, most likely because of the name conflict.

Problem solved!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.