0

I am developing a theme and am getting the following error "Warning: trim() expects parameter 1 to be string, array given in C:\wamp\www\themes\wp-includes\query.php on line 1609". I have tried everything I could think of and have Googled for longer than I'd like to admit trying to get it sorted out.

The Issue

I have created a form that allows users to submit a recipe and submit it to a custom post type using wp_insert_post on the front end of the site. The problem is with the repeating fields that I have set up. They are working fine in the Dashboard but had to obviously be modified. They submit to an array and are submitting correctly to my post type but keep throwing the error above.

I would be grateful if anyone could take a look and let me know what I could do to clean it up.

Not sure if it matters but here is how I am duplicating the fields

<script type="text/javascript">
                jQuery(document).ready(function( $ ){
                    $( '#add-row' ).on('click', function() {
                        var row = $( '.empty-row.screen-reader-text' ).clone(true);
                        row.removeClass( 'empty-row screen-reader-text' );
                        row.insertBefore( '#repeatable-ingredient-set tbody>tr:last' );
                        return false;
                    });

                    $( '.remove-button' ).on('click', function() {
                        $(this).parents('tr').remove();
                        return false;
                    });
                });
</script>

Here is the section that's causing the issue

<!-- Add the repeating ingredient fields -->
                <tr>
                    <td><input type="text" class="widefat" name="name[]" /></td>

                    <td><input type="text" class="widefat" name="amount[]" /></td>

                    <td>
                        <select name="unit[]">
                        <?php foreach ( $options as $label => $value ) : ?>
                        <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
                        <?php endforeach; ?>
                        </select>
                    </td>

                    <td><a class="button remove-button" href="#">Remove</a></td>
                </tr>

                <!-- Hidden field group used when add button is pressed (jQuery) -->
                <tr class="empty-row screen-reader-text">
                    <td><input type="text" class="widefat" name="name[]" /></td>

                    <td><input type="text" class="widefat" name="amount[]" /></td>

                    <td>
                        <select name="unit[]">
                        <?php foreach ( $options as $label => $value ) : ?>
                        <option value="<?php echo $value; ?>"><?php echo $label; ?></option>
                        <?php endforeach; ?>
                        </select>
                    </td>

                    <td><a class="button remove-button" href="#">Remove</a></td>
                </tr>

Here is where it gets submitted

//* Create a blank array for the newly added ingredients
    $ingredients = array();
    //* Get the names
    $names = $_POST['name'];
    //* Get the measurements
    $amounts = $_POST['amount'];
    //* Get the units ($options)
    $units = $_POST['unit'];
    //* Find the number of ingredients by counting the names
    $count = count( $names );

    //* As long as there are ingredients then add them to the array
    for ( $i = 0; $i < $count; $i++ ) {
        if ( $names[$i] != '' ) :
            $ingredients[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );
            $ingredients[$i]['amount'] = stripslashes( strip_tags( $amounts[$i] ) );

            if ( in_array( $units[$i], $options ) )
                $ingredients[$i]['unit'] = $units[$i];
            else
                $ingredients[$i]['unit'] = '';
        endif;  
    }

    //* If all requirements are met then proceed to build the new recipe
    if ( isset($_POST['recipe_title']) && isset($_POST['recipe_content']) && ( isset($hasError) ==false )) {

        global $wpdb;

        //* Add all the information collected from the form
        $new_post = array(
        'post_title'        =>  $recipe_title, //* Add the title
        'post_content'      =>  $recipe_content, //* Add the instructions
        'meta_input'        => array(
                            'cook_time_hour'    =>  $cook_time_hour, //* Add the cook time hours
                            'cook_time_minute'  =>  $cook_time_minute, //* Add the cook time minutes
                            'servings'          =>  $servings, //* Add the servings
                            'calories'          =>  $calories, //* Add the calories
                            'measurement_units' =>  $ingredients, //* Add the ingredients
            ),
        'tax_input'         => array(
                           'recipe_types' => array( $recipe_cat[0] ), //* Add the recipe type
            ),
        'post_status'       =>  'pending', //* Set the new recipe as pending to be reviewed
        'post_type'         =>  'recipes' //* Set it to the recipes post type
        );

        //* Get the post ID of the newly created recipe
        $post_id = wp_insert_post($new_post);

I know that the name attributes, name[], amount[], and unit[], are the problem but not sure if there is a way to fix this so that it plays nice with WordPress without heavy modifications.

Thanks in advance!

  • 3
    Can you show the piece of code that actually handles the submission process on the backend? The one that directly causes the warning to appear. Also, what WP version are you using? This is important to see what exactly is on line 1609. – Mateusz Hajdziony Oct 5 '16 at 23:48
  • I have updated my question with the code I use to submit the fields. Running WordPress Version 4.6.1. – WP Coder Oct 6 '16 at 16:18
0

I think 'measurement_units' => $ingredients, causes the problem, because the $ingredients is multi-dimensional array. You should use i.e. 'measurement_units' => $ingredients[0], or etc.. i think.

  • I tired it but it no longer captured the data properly and in the post admin screen I got Warning: Illegal string offset 'name' in C:\wamp\www\themes\wp-content\themes\recipes-pro\inc\post-types\recipes-cpt.php on line 244. I got this for all fields submitted. – WP Coder Oct 6 '16 at 21:16
0

Your warning is because you apply a trim() on an array. The fact to add double bracket (i.e: unit[]) to input name, convert it to an array.

You need to add an indexed block when you add a repeatable element, and then add custom field (or update post), this could be great if you want to sort your element.

  • In order for the ingredients to submit properly in my custom post type repeater field these need to be submitted to a single variable. The trim() is being applied by WordPress, it's not anywhere in my code. The code I have now is working perfect and the ingredients are being added properly except I get a warning. – WP Coder Oct 7 '16 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.