WordPress正在发出警告,但我的代码运行正常

时间:2016-10-05 作者:WP Coder

我正在开发一个主题,出现以下错误“Warning: trim() expects parameter 1 to be string, array given in C:\\wamp\\www\\themes\\wp-includes\\query.php on line 1609“我已经尝试了我能想到的一切,并且在谷歌上搜索的时间比我想承认的要长得多。

The Issue

我创建了一个表单,允许用户提交配方并使用wp_insert_post 在网站的前端。问题是我设置的重复字段。它们在仪表板上工作正常,但显然需要进行修改。他们提交到一个数组,并正确提交到我的帖子类型,但不断抛出上面的错误。

如果有人能看一下,让我知道我能做些什么来清理它,我将不胜感激。

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);
我知道名字的属性,name[], amount[], 和unit[], 是问题所在,但不确定是否有办法解决此问题,以便在不进行大量修改的情况下使用WordPress。

提前感谢!

2 个回复
最合适的回答,由SO网友:T.Todua 整理而成

我想\'measurement_units\' => $ingredients, 导致问题,因为$ingredients 是多维数组。您应该使用,即。\'measurement_units\' => $ingredients[0], 或等等。。我想。

SO网友:Benoti

您的警告是因为您对数组应用了trim()。要在输入名称中添加双括号(即:unit[]),请将其转换为数组。

当您添加可重复的元素时,需要添加索引块,然后添加自定义字段(或更新帖子),如果您想对元素进行排序,这将非常有用。

相关推荐

PHPStorm variable warnings

我在PHPStorm中编码,并在路径中包含wp admin和wp include,但会收到与未定义/未使用变量相关的代码检查警告。我应该担心这些吗?我如何满足解决错误的解决方案(而不只是关闭检查)