[wp-hackers] Problems with reference to $wp_roles->roles

David F. Carr david at carrcommunications.com
Thu Feb 9 15:35:51 UTC 2012

I've run into problems with some code that uses a foreach loop on
$wp_roles->roles - the array of roles in the $wp_roles global.

Worked fine in my testing but caused all sorts of problems for some other
users of my Facebook Tab Manager plugin. The code I originally released
failed to check for the existence of the array, and people got PHP errors
from iterating on a non-existent array. I figured it was a global, so it's
got to be there, right? Apparently not.

I've semi-fixed this with an update, which tests to make sure the variable
is set first. To fix it right, I probably need to understand what's going

Is the $wp_roles->roles variable only set under certain circumstances, like
when a user is logged in? I tried to test that by logging out and browsing
my test site, but I still didn't see the errors other people are seeing.

Context: This is related to a function that sets the roles and capabilities
for my custom post type to parallel those of regular posts. Depending on an
options setting, editing rights may or may not be assigned to authors and

David F. Carr
Writer, Editor, Web Consultant
david at carrcommunications.com

Editor, The BrainYard http://www.informationweek.com/thebrainyard
LinkedIn - http://www.linkedin.com/in/davidfcarr
Facebook - http://www.facebook.com/carrcomm

More information about the wp-hackers mailing list