WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Cannot use a scalar value as an array error in post.php (11 posts)

  1. audwan
    Member
    Posted 4 years ago #

    I got these errors under Last Comments in the dashboard after I upgraded to 2.8.1:

    Warning: Cannot use a scalar value as an array in /home/foo/public_html/bar/wp-includes/post.php on line 820

    Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/foo/public_html/bar/wp-includes/post.php on line 821

    Warning: Invalid argument supplied for foreach() in /home/foo/public_html/bar/wp-includes/post.php on line 821

    Warning: Cannot use a scalar value as an array in /home/foo/public_html/bar/wp-includes/post.php on line 823

    They seem to appear only on my own comments.

    After changing a line of code in post.php the error messages disappeared. But as I'm not exactly a PHP guru (that's an understatement), and this probably changes the behaviour of the blog when it comes to sanitation of inputs, I'd really like some help looking into it.

    Is this a bug in WordPress, data corruption or something else? Any help would be appreciated.

    Heres the original:

    function sanitize_post($post, $context = 'display') {
    	if ( is_object($post) ) {
    		if ( !isset($post->ID) )
    			$post->ID = 0;
    		foreach ( array_keys(get_object_vars($post)) as $field )
    			$post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
    		$post->filter = $context;
    	} else {
    		if ( !isset($post['ID']) )
    			$post['ID'] = 0;
    		foreach ( array_keys($post) as $field )
    			$post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
    		$post['filter'] = $context;
    	}
    
    	return $post;
    }

    And this is what it looks like now:

    function sanitize_post($post, $context = 'display') {
    	if ( is_object($post) ) {
    		if ( !isset($post->ID) )
    			$post->ID = 0;
    		foreach ( array_keys(get_object_vars($post)) as $field )
    			$post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
    		$post->filter = $context;
    	} else if ( is_array($post) ) {
    		if ( !isset($post['ID']) )
    			$post['ID'] = 0;
    		foreach ( array_keys($post) as $field )
    			$post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
    		$post['filter'] = $context;
    	}
    
    	return $post;
    }
  2. audwan
    Member
    Posted 4 years ago #

    Same error appear in 2.8.2. Just in the Dashboard, and just on my own comments.

  3. iBinBin
    Member
    Posted 4 years ago #

    Hello,

    I've got the same proleme as you in the plugin page, no plugin are activated and i cant see there name and description!!!

    Warning: Cannot use a scalar value as an array in /homez.22/jvglobet/www/wp-admin/includes/plugin.php on line 123

    Warning: Cannot use a scalar value as an array in /homez.22/jvglobet/www/wp-admin/includes/plugin.php on line 128

    Warning: Cannot use a scalar value as an array in /homez.22/jvglobet/www/wp-admin/includes/plugin.php on line 136

    Do you have any ideas about it, did you solved your probleme?

    Regard,

  4. audwan
    Member
    Posted 4 years ago #

    Sorry, I still have no fix other than the one in the first post.

    Updated to 2.8.3, and the error is still there.

  5. bishopj
    Member
    Posted 4 years ago #

    Which version of PHP are your running?

    As it stands, if the $post value is neither an object nor an array you will simply pass it through. Prima-facie your code is partially better, except that there is an unhandled condition - that of a scalar value for $post that was clearly meant not to arise.

    The error is therefore before that function - not in that function.

    Which suggests one of three things:
    1. You have code mods else where that are wrong, or the upgrade did not replace all files in the original WP diretories
    2. The database upgrade did not take, so you have a wrong type for that field (unlikely).
    3. In the words of rumsfield...there are things that we know, and things we know we don't know, and things we don't know that we don't know...

    I suggest looking at what calls that routine. My install of 2.8.3 does not exhibit this error (running on PHP 5). Handling of input fields from post/get actions were one of the big messy changes made between PHP 3 and 4, and then cleaned up in early releases of 4.

    I would first make sure that you have the version of php running that you think you have (although I would expect more extensive problems than this if you had the wrong version).

    The standard advice with 2.8.3 seems to be to disable all plugins and then progressively re-enable them until the error resurfaces.

  6. bishopj
    Member
    Posted 4 years ago #

    Actually, thinking about this - when the error occurs, what exactly is the value of $post? Does it have a value, or has it been passed 0 or NULL?

    This would suggest that the routine is being called once too many times - particularly if the comments are otherwise appearing.

  7. audwan
    Member
    Posted 4 years ago #

    Thanks for the answer, bishopj. I appreciate any help, or just a small push in the right direction.

    The comments do indeed appear on the site, and the errors occur only on the comments from users that are logged in. I tried to add a new user (subscriber), and added a comment with that user, and it produced the same error in the Dashboard. That should narrow it down a bit, but I still got no clue how to fix it.

    The blog runs on PHP 5.2.8 with MySQL 5.1.30. Other blogs running on the exact same platform don't have the same issue, and some do.

    I tried reinstalling all WordPress files and disabled all my plug-ins, but the problem still persists. I even checked the database to see if there was any issues with those specific comments, but didn't find anything suspicious.

    Is there any way I can output the value or type of $post? That would probably help.

  8. audwan
    Member
    Posted 4 years ago #

    I've been doing some n00b-debugging, just to try getting to the bottom of this problem.

    I replaced the else-line from the first post with:
    } else if ( !is_null($post) ) {
    and still get errors. That should suggest that $post being NULL is not the issue, right?

    Then I tried:
    } else if ( is_scalar($post['ID']) ) {
    and the errors disappeard.

    So $post['ID'] is not a scalar value when the error occurs, which means it is either array, object or resource (or perhaps not present)? (ref: PHP:is_scalar)

    As a last resort I tried:
    } else if ( !is_array($post['ID']) ) {
    } else if ( !is_object($post['ID']) ) {
    } else if ( !is_resource($post['ID']) ) {
    but I get errors on all of them, which is where my logic breaks.

    Am I missing something here?

  9. Chris Burbridge
    Member
    Posted 4 years ago #

    Please note that this error, "Warning: array_keys() [function.array-keys]: The first argument should be an array" can also be caused by the following:

    * WP asks you for ftp information to install a theme or plugin, when you click Install.
    * The user you are referring to does not have sufficient permissions to do this

    Update:

    This fixed *part* of the problem, so now it uploads the files, but I am still getting another error, FYI.

  10. audwan
    Member
    Posted 4 years ago #

    The errors suddenly disappeared when I upgraded to WordPress 2.9.

  11. jason@jinteractive.net
    Member
    Posted 4 years ago #

    This isn't an error. It's a warning, and the developers probably don't consider it a bug. Unless you're in a testing environment, warnings should be supressed by your webserver. Printing stuff like that to the screen can be a serious security issue.

    I supress these warnings in my own functions by preceding the offending statement with the '@' symbol. Usually everything works fine, save for the weird message breaking the layout.

Topic Closed

This topic has been closed to new replies.

About this Topic