Previous post: Back to San Francisco?

Next post: Lancement du blog de Josef Zisyadis

Invalid argument supplied for foreach() in wp-capabilities.php: Case Cracked!

by Stephanie on 01.29.2007

in Weblog Technology

[fr]

Le problème avec wp-capabilities.php qui fait qu'on peut se retrouver "exfermé" (enfermé dehors) de son blog WordPress (typiquement en cas de changement de serveur) semble avoir sa source dans le contenu du champ wp_user_roles dans la table wp_options. En particulier, pour la version française, "Abonné" est un rôle d'utilisateur, et en cas de problèmes d'encodage MySQL, le caractère accentué sera corrompu, causant ainsi l'erreur.

Il suffit de remplacer le caractère fautif dans PhpMyAdmin, et on retrouve l'accès à son blog. Bon, reste ensuite à régler les questions d'encodage... mais c'est déjà ça!

[en]

Finally. At last. Endlich. Enfin.

Once more, while trying to transfer a WordPress installation from one server to another, I found myself facing the dreaded problem which locks me out of my WordPress install with a rather cryptic message:

Warning: Invalid argument supplied for foreach() in /home/user/wp/wp-includes/capabilities.php on line 31

(Your lineage may vary.)

What happens is that WordPress cannot read user roles, and therefore, even though your password is accepted, you get a message telling you that you’re not welcome in the wp-admin section:

Vous n’avez pas les droits suffisants pour accéder à cette page.

Or, in English:

You do not have sufficient permissions to access this page.

A quick search on the WordPress forums told me that I was not alone in my fight with wp-capabilities.php, but that many problems had not been resolved, and more importantly, that suggested solutions often did not work for everyone.

I’ve bumped into this problem a couple of times before, and I knew that it was linked to encoding problems in the database. (I’ve had my share of encoding problems: once, twice, thrice — “once” being on of the most-visited posts on this blog, by the way, proof if needed that I’m not alone with mysql encoding issues either.)

I’ll leave the detailed resolution of how to avoid/cure the MySQL problems later (adding mysql_query("SET NAMES 'utf8'"); to wp-db.php as detailed in this thread, and as zedrdave had already previously told me to do — should have listened! — should prevent them). So anyway, adding that line to my working WordPress install showed me that the problem was not so much in the database dumping process than in the way WordPress itself interacted with the database, because the dreaded wp-capabilities.php problem suddenly appeared on the original blog.

Now, this is where I got lucky. Browsing quickly through the first dozen or so of forum threads about wp-capability.php problems, this response caught my eye. It indicated that the source of the problem was the content of the wp_user_roles field (your prefix may vary). In this case, it had been split on more than one line.

I headed for the database, looked at the field, and didn’t see anything abnormal about it at first. All on one line, no weird characters… just before giving up, I moved the horizontal scrollbar to the end of the line, and there — Eurêka! I saw it.

Abonné

“Contributor”, in French, is “abonné”, with an accent. Accent which got horribly mangled by the MySQL problems which I’ll strive to resolve shorty. Mangled character which caused the foreach() loop to break in wp-capabilities.php, which caused the capabilities to not be loaded, which caused me to be locked out of my blog.

So, in summary: if you’re locked out of your blog and get a warning/error about wp-capabilities and some invalid foreach() loop thingy, head for PhpMyAdmin, and look carefully through the wp_user_roles field in the wp_options table. If it’s split over two or more lines, or contains funky characters, you have probably found the source of your problem.

Good luck!

{ 2 trackbacks }

点点游 | WordPress 中文版下载| WordPress 中文文档 » Blog 存档 » Warning: Invalid argument supplied for foreach() 的解决方法
03.19.2007 at 4:46
wordpress错误Warning: Invalid argument supplied for foreach() 的解决方法——P268博客
03.22.2007 at 14:22

{ 31 comments… read them below or add one }

1 Xavier 01.29.2007 at 17:21

Mmmh, would say me changing the translation file to say “Abonné” instead of “Abonné” would solve the problem for future generations ?

2 Stephanie 01.29.2007 at 17:28

It would. eacute. What would also solve the problem would be adding that utf-8 line in wp-db.php. Will try to catch the WP people about that.

3 Xavier 01.29.2007 at 17:34

MO file : done, for both 2.0.7 & 2.1 (both utf-8 & iso-8859-1).
Let me know - if you still reproduce de problem.

Also, try to file a bug report to the Trac (free registration ! ;) )
http://trac.wordpress.org/timeline</p>

4 Xavier 01.29.2007 at 17:38

Pfioulala, je pense trop vite pour mes doigts. Donc, pour la totale : +you, +can, s/de/the/ :)

5 Xavier 01.29.2007 at 18:21

Mmmh, would say me changing the translation file to say “Abonné” instead of “Abonné” would solve the problem for future generations ?

6 Stephanie 01.29.2007 at 18:28

It would. eacute. What would also solve the problem would be adding that utf-8 line in wp-db.php. Will try to catch the WP people about that.

7 Xavier 01.29.2007 at 18:34

MO file : done, for both 2.0.7 & 2.1 (both utf-8 & iso-8859-1).
Let me know - if you still reproduce de problem.

Also, try to file a bug report to the Trac (free registration ! ;) )
http://trac.wordpress.org/timeline

8 Xavier 01.29.2007 at 18:38

Pfioulala, je pense trop vite pour mes doigts. Donc, pour la totale : +you, +can, s/de/the/ :)

9 Lloyd Budd 01.30.2007 at 22:03

It is a good solution if you use UTF8, but not everyone is. WordPress core seems to need a more comprehensive solution.

10 Lloyd Budd 01.30.2007 at 23:03

It is a good solution if you use UTF8, but not everyone is. WordPress core seems to need a more comprehensive solution.

11 David Touvet 02.04.2007 at 21:15

Salut Steph'
j'ai eu également une l'erreur suivante en upgradant à la version WP 2.1

Fatal error: Call to undefined function: wp_get_current_user() in /home/cybexpor/public_html/wp-includes/capabilities.php

Ca ressemble un peu à ton erreur. Ce qui m'a sauvé fut simplement de reuploader sur le serveur le répertoire wp-includes…

A jeudi!
david

12 David Touvet 02.04.2007 at 22:15

Salut Steph’
j’ai eu également une l’erreur suivante en upgradant à la version WP 2.1

Fatal error: Call to undefined function: wp_get_current_user() in /home/cybexpor/public_html/wp-includes/capabilities.php

Ca ressemble un peu à ton erreur. Ce qui m’a sauvé fut simplement de reuploader sur le serveur le répertoire wp-includes…

A jeudi!
david

13 Zomp 04.11.2007 at 16:32

Many thanks, you helped me very much!

14 Zomp 04.11.2007 at 16:32

Many thanks, you helped me very much!

15 N. Naci Kisnisci 05.21.2007 at 13:16

Thank you for solution and sharing

16 N. Naci Kisnisci 05.21.2007 at 13:16

Thank you for solution and sharing

17 Andrew 01.07.2008 at 14:52

As a total n00b to WP and PHP I cannot tell what in my table might be causing this problem. I've managed to dig this up from PHPMyAdmin; I think it is the correct field:

a:5:{s:13:”administrator”;a:2:{s:4:”name”;s:13:”Administrator”;s:12:”capabilities”;a:48:{s:13:”switch_themes”;i:1;s:11:”edit_themes”;i:1;s:16:”activate_plugins”;i:1;s:12:”edit_plugins”;i:1;s:10:”edit_users”;i:1;s:10:”edit_files”;i:1;s:14:”manage_options”;i:1;s:17:”moderate_comments”;i:1;s:17:”manage_categories”;i:1;s:12:”manage_links”;i:1;s:12:”upload_files”;i:1;s:6:”import”;i:1;s:15:”unfiltered_html”;i:1;s:10:”edit_posts”;i:1;s:17:”edit_others_posts”;i:1;s:20:”edit_published_posts”;i:1;s:13:”publish_posts”;i:1;s:10:”edit_pages”;i:1;s:4:”read”;i:1;s:8:”level_10″;i:1;s:7:”level_9″;i:1;s:7:”level_8″;i:1;s:7:”level_7″;i:1;s:7:”level_6″;i:1;s:7:”level_5″;i:1;s:7:”level_4″;i:1;s:7:”level_3″;i:1;s:7:”level_2″;i:1;s:7:”level_1″;i:1;s:7:”level_0″;i:1;s:17:”edit_others_pages”;i:1;s:20:”edit_published_pages”;i:1;s:13:”publish_pages”;i:1;s:12:”delete_pages”;i:1;s:19:”delete_others_pages”;i:1;s:22:”delete_published_pages”;i:1;s:12:”delete_posts”;i:1;s:19:”delete_others_posts”;i:1;s:22:”delete_published_posts”;i:1;s:20:”delete_private_posts”;i:1;s:18:”edit_private_posts”;i:1;s:18:”read_private_posts”;i:1;s:20:”delete_private_pages”;i:1;s:18:”edit_private_pages”;i:1;s:18:”read_private_pages”;i:1;s:12:”delete_users”;i:1;s:12:”create_users”;i:1;s:17:”unfiltered_upload”;i:1;}}s:6:”editor”;a:2:{s:4:”name”;s:6:”Editor”;s:12:”capabilities”;a:34:{s:17:”moderate_comments”;i:1;s:17:”manage_categories”;i:1;s:12:”manage_links”;i:1;s:12:”upload_files”;i:1;s:15:”unfiltered_html”;i:1;s:10:”edit_posts”;i:1;s:17:”edit_others_posts”;i:1;s:20:”edit_published_posts”;i:1;s:13:”publish_posts”;i:1;s:10:”edit_pages”;i:1;s:4:”read”;i:1;s:7:”level_7″;i:1;s:7:”level_6″;i:1;s:7:”level_5″;i:1;s:7:”level_4″;i:1;s:7:”level_3″;i:1;s:7:”level_2″;i:1;s:7:”level_1″;i:1;s:7:”level_0″;i:1;s:17:”edit_others_pages”;i:1;s:20:”edit_published_pages”;i:1;s:13:”publish_pages”;i:1;s:12:”delete_pages”;i:1;s:19:”delete_others_pages”;i:1;s:22:”delete_published_pages”;i:1;s:12:”delete_posts”;i:1;s:19:”delete_others_posts”;i:1;s:22:”delete_published_posts”;i:1;s:20:”delete_private_posts”;i:1;s:18:”edit_private_posts”;i:1;s:18:”read_private_posts”;i:1;s:20:”delete_private_pages”;i:1;s:18:”edit_private_pages”;i:1;s:18:”read_private_pages”;i:1;}}s:6:”author”;a:2:{s:4:”name”;s:6:”Author”;s:12:”capabilities”;a:10:{s:12:”upload_files”;i:1;s:10:”edit_posts”;i:1;s:20:”edit_published_posts”;i:1;s:13:”publish_posts”;i:1;s:4:”read”;i:1;s:7:”level_2″;i:1;s:7:”level_1″;i:1;s:7:”level_0″;i:1;s:12:”delete_posts”;i:1;s:22:”delete_published_posts”;i:1;}}s:11:”contributor”;a:2:{s:4:”name”;s:11:”Contributor”;s:12:”capabilities”;a:6:{s:10:”edit_posts”;i:1;s:20:”edit_published_posts”;i:1;s:4:”read”;i:1;s:7:”level_1″;i:1;s:7:”level_0″;i:1;s:12:”delete_posts”;i:1;}}s:10:”subscriber”;a:2:{s:4:”name”;s:10:”Subscriber”;s:12:”capabilities”;a:2:{s:4:”read”;i:1;s:7:”level_0″;i:1;}}}

Does anyone know if I am looking at the right thing, and if so, what might the actual problem be? I don't see any mangled characters.

Thanks to anyone who can help.

Andrew

18 Andrew 01.07.2008 at 15:40

ah I see my row is not wrapping - sorry. I don't see anything that looks wrong in it but will keep at it. If I find anything will post here and on the WP forums.

19 Andrew 01.07.2008 at 15:52

As a total n00b to WP and PHP I cannot tell what in my table might be causing this problem. I’ve managed to dig this up from PHPMyAdmin; I think it is the correct field:

a:5:{s:13:”administrator”;a:2:{s:4:”name”;s:13:”Administrator”;s:12:”capabilities”;a:48:{s:13:”switch_themes”;i:1;s:11:”edit_themes”;i:1;s:16:”activate_plugins”;i:1;s:12:”edit_plugins”;i:1;s:10:”edit_users”;i:1;s:10:”edit_files”;i:1;s:14:”manage_options”;i:1;s:17:”moderate_comments”;i:1;s:17:”manage_categories”;i:1;s:12:”manage_links”;i:1;s:12:”upload_files”;i:1;s:6:”import”;i:1;s:15:”unfiltered_html”;i:1;s:10:”edit_posts”;i:1;s:17:”edit_others_posts”;i:1;s:20:”edit_published_posts”;i:1;s:13:”publish_posts”;i:1;s:10:”edit_pages”;i:1;s:4:”read”;i:1;s:8:”level_10″;i:1;s:7:”level_9″;i:1;s:7:”level_8″;i:1;s:7:”level_7″;i:1;s:7:”level_6″;i:1;s:7:”level_5″;i:1;s:7:”level_4″;i:1;s:7:”level_3″;i:1;s:7:”level_2″;i:1;s:7:”level_1″;i:1;s:7:”level_0″;i:1;s:17:”edit_others_pages”;i:1;s:20:”edit_published_pages”;i:1;s:13:”publish_pages”;i:1;s:12:”delete_pages”;i:1;s:19:”delete_others_pages”;i:1;s:22:”delete_published_pages”;i:1;s:12:”delete_posts”;i:1;s:19:”delete_others_posts”;i:1;s:22:”delete_published_posts”;i:1;s:20:”delete_private_posts”;i:1;s:18:”edit_private_posts”;i:1;s:18:”read_private_posts”;i:1;s:20:”delete_private_pages”;i:1;s:18:”edit_private_pages”;i:1;s:18:”read_private_pages”;i:1;s:12:”delete_users”;i:1;s:12:”create_users”;i:1;s:17:”unfiltered_upload”;i:1;}}s:6:”editor”;a:2:{s:4:”name”;s:6:”Editor”;s:12:”capabilities”;a:34:{s:17:”moderate_comments”;i:1;s:17:”manage_categories”;i:1;s:12:”manage_links”;i:1;s:12:”upload_files”;i:1;s:15:”unfiltered_html”;i:1;s:10:”edit_posts”;i:1;s:17:”edit_others_posts”;i:1;s:20:”edit_published_posts”;i:1;s:13:”publish_posts”;i:1;s:10:”edit_pages”;i:1;s:4:”read”;i:1;s:7:”level_7″;i:1;s:7:”level_6″;i:1;s:7:”level_5″;i:1;s:7:”level_4″;i:1;s:7:”level_3″;i:1;s:7:”level_2″;i:1;s:7:”level_1″;i:1;s:7:”level_0″;i:1;s:17:”edit_others_pages”;i:1;s:20:”edit_published_pages”;i:1;s:13:”publish_pages”;i:1;s:12:”delete_pages”;i:1;s:19:”delete_others_pages”;i:1;s:22:”delete_published_pages”;i:1;s:12:”delete_posts”;i:1;s:19:”delete_others_posts”;i:1;s:22:”delete_published_posts”;i:1;s:20:”delete_private_posts”;i:1;s:18:”edit_private_posts”;i:1;s:18:”read_private_posts”;i:1;s:20:”delete_private_pages”;i:1;s:18:”edit_private_pages”;i:1;s:18:”read_private_pages”;i:1;}}s:6:”author”;a:2:{s:4:”name”;s:6:”Author”;s:12:”capabilities”;a:10:{s:12:”upload_files”;i:1;s:10:”edit_posts”;i:1;s:20:”edit_published_posts”;i:1;s:13:”publish_posts”;i:1;s:4:”read”;i:1;s:7:”level_2″;i:1;s:7:”level_1″;i:1;s:7:”level_0″;i:1;s:12:”delete_posts”;i:1;s:22:”delete_published_posts”;i:1;}}s:11:”contributor”;a:2:{s:4:”name”;s:11:”Contributor”;s:12:”capabilities”;a:6:{s:10:”edit_posts”;i:1;s:20:”edit_published_posts”;i:1;s:4:”read”;i:1;s:7:”level_1″;i:1;s:7:”level_0″;i:1;s:12:”delete_posts”;i:1;}}s:10:”subscriber”;a:2:{s:4:”name”;s:10:”Subscriber”;s:12:”capabilities”;a:2:{s:4:”read”;i:1;s:7:”level_0″;i:1;}}}

Does anyone know if I am looking at the right thing, and if so, what might the actual problem be? I don’t see any mangled characters.

Thanks to anyone who can help.

Andrew

20 Andrew 01.07.2008 at 16:40

ah I see my row is not wrapping - sorry. I don’t see anything that looks wrong in it but will keep at it. If I find anything will post here and on the WP forums.

21 Christian 01.09.2008 at 20:33

Thank you, thank you, thank you! The User roles area was where I was having troubles.

Cheerful Trails,

Christian

22 Christian 01.09.2008 at 21:33

Thank you, thank you, thank you! The User roles area was where I was having troubles.

Cheerful Trails,

Christian

23 Aoife 02.07.2008 at 14:41

You are a life saver, thank you! I looked for hours and even my hosting company was not able to help… now it's fixed!

24 Aoife 02.07.2008 at 15:41

You are a life saver, thank you! I looked for hours and even my hosting company was not able to help… now it’s fixed!

25 Yedidia 02.14.2008 at 23:32

you guys are much too good for me.

“So, in summary: if you’re locked out of your blog and get a warning/error about wp-capabilities and some invalid foreach() loop thingy, head for PhpMyAdmin, and look carefully through the wp_user_roles field in the wp_options table. If it’s split over two or more lines, or contains funky characters, you have probably found the source of your problem.”

so it is scrambled (abonné). what should I do?

26 Yedidia 02.14.2008 at 23:42

Bon, j'avoue, j'ai paniqué. Donc je ne suis plus exfermée sur mon blog. Y a-t-il une manière simple de modifier tous les caractères “scramblés” sur mon blog?
D'autant que j'en ai d'autres à upgrader , et autrement plus verbeux.
Merci de m'avoir fourni la solution, quand même…

27 Yedidia 02.15.2008 at 0:32

you guys are much too good for me.

“So, in summary: if you’re locked out of your blog and get a warning/error about wp-capabilities and some invalid foreach() loop thingy, head for PhpMyAdmin, and look carefully through the wp_user_roles field in the wp_options table. If it’s split over two or more lines, or contains funky characters, you have probably found the source of your problem.”

so it is scrambled (abonné). what should I do?

28 Yedidia 02.15.2008 at 0:42

Bon, j’avoue, j’ai paniqué. Donc je ne suis plus exfermée sur mon blog. Y a-t-il une manière simple de modifier tous les caractères “scramblés” sur mon blog?
D’autant que j’en ai d’autres à upgrader , et autrement plus verbeux.
Merci de m’avoir fourni la solution, quand même…

29 Tamer Salama 04.26.2008 at 0:41

PHP initial configuration values should be as follows:

magic_quotes_runtime = off;
magic_quotes_gpc = off;
register_globals = on;

via:
http://wordpress.org/support/topic/122367

30 Tamer Salama 04.26.2008 at 0:41

PHP initial configuration values should be as follows:

magic_quotes_runtime = off;
magic_quotes_gpc = off;
register_globals = on;

via:
http://wordpress.org/support/topic/122367</p>

31 Amaury 12.17.2008 at 14:23

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

« Back to text comment

Additional comments powered by BackType

Clicky Web Analytics