Hi Cam,
You seem like a long-time user of S2, and probably pretty good at coding. Could I trouble you to have a quick look at my code, which is not yet giving me the results I’m seeking? I’ve managed to get the cron event to work, and with the help of the plugin “Crontrol”, I can fire it immediately for testing purposes. I also figured out how to print out results, by setting DEBUG flag in wp-config.php file and creating a log file. I read all the users from WP database (just some fake ones for the moment), and I’m now trying to create two arrays of users, one array is filled with the users that have paid or are “courtesy” (free lifetime membership, i.e. no eot) and who have listed a spouse email in S2 custom field. The other array is the users who are the spouses, whom I call “conditional” members, and they have an invisible S2 custom field (for admin eyes only) called the master ID, which is the user ID of the paid or courtesy member they are tied to. It’s like a foreign key in a database. So, level0 = non members (but I have their email), level1 = conditional members, level2 = paid members (through paypal or by adding them by hand if paid by check), and level3 = courtesy members.
Anyway, the filling of the arrays seems to be going well, but now I want to start updating user info and creating new members programmatically. Specifically, if there’s a paid/courtesy member with a spouse email, then I’ll check that there’s a conditional member tied to them, and if not then create new conditional member with the appropriate masterID custom field added to their info. Updates can also occur, if the paid/courtesy member changes their profile and sticks in a new spouse email. In that case, I’ll find the conditional member with the masterID, and update their email.
But, the problem I’m having is that the updating of user isn’t working as expected. For example, as you’ll see in the code, I do a quick error check to see if there are any conditional members that have no masterID in their custom field, and if I find one, then demote them down to level0 (non member). To test it out, in Admin Panel I created a new user and chose their role to be conditional member (level1), but didn’t put in any masterID. The code does indeed recognize this and tries to demote them. The trouble is, when I go look at that user later in the Admin Panel, it has no role, and at the very bottom of user profile page, it has a new note saying there are “added capabilities”, this note not being present on any other users.
I found this S2 tutorial on this subject, here it is:
https://s2member.com/kb-article/rolescapabilities-via-php/
But, this tutorial is rather confusing, probably due to my limited php knowledge. For example, why do they instantiate the user with $user = new WP_User($user->ID); ? Isn’t $user already instantiated because of the foreach loop?
Okay, so here’s my code, I put in lots of comments so it should be apparent what I’m trying to do. The code is in s2-hacks.php, which resides in mu-plugins. I’d be very grateful if you could take a look at it, and you’ll probably find some errors I’m making. Thank-you!
Later edit: code appears in next comment