WPUser->add_role not really adding role
-
I’ve put together a script to update roles on a number of users on my wordpress site. It reads a simple CSV file to know what role or roles to give to a particular user, looks up that user by their user ID, and then checks to see if they have a particular role already, adding it if they don’t.
error_reporting(E_ALL); require("../../wp-load.php"); $source_file = 'role_source'; if(($source_handle = fopen($source_file, "r")) !== false) { while(($row = fgetcsv($source_handle)) !== false) { if(count($row) > 2) { if(($user = get_user_by('id', $row[1])) !== false) { for($i = 2; $i < count($row); $i++) { if(!$user->has_cap($row[$i])) { $user->add_role($row[$i]); } } } } } fclose($source_handle); }
If I check the users in the database afterwards their wp_capabilities entries in wp_usermeta have been updated, but that’s all. In my template when I call current_user_can() with one of the roles that should have been added to my test user (and that I can see listed in the database) it returns false. So it looks like there’s something else that should be happening that isn’t, or something else I should be doing that I don’t know about.
- The topic ‘WPUser->add_role not really adding role’ is closed to new replies.