I am currently tweaking a plugin so that all "contributors" (here referred to as "players") are automatically subscribed to a forum. I basically want to subscribe everyone with the role of contributor and above (I've used user_level because the rest of the plugin does and I thought I should be consistent.)
function forum_subscribe_member_player()
{
$players= get_users();
foreach($players as $player){
$playeremail = get_userdata($player->ID, 'user_email');
if(!empty($playeremail) && $player->user_level >= 1)
{
$list = get_option("mf_forum_subscribers_1", array());
if( is_player_subscribed($player->ID)) //remove user if already exists (user clicked unsubscribe)
{
$key = array_search($playeremail, $list);
unset($list[$key]);
}
else
$list[] = $playeremail;
update_option("mf_forum_subscribers_1", $list);
}
}
}
Its not working and I'm guessing there is a problem with $player->is_player_subscribed()
which is here :
function is_player_subscribed($user_ID)
{
if($user_ID)
{
$useremail = get_userdata($user_ID, 'user_email');
$list = get_option("mf_forum_subscribers_1", array());
if(in_array($useremail, $list)){
return true;
}
return false;
}
}
Which uses the global $user_ID
rather than get_users()
I would love some help with this or even to be pointed in the right direction.
EDIT: based on comment by @murdaugh
$player->is_forum_subscribed()
would imply thatis_forum_subscribed()
is a public function (method) of the WP_User class. It's not. My recommendation would be to refactor that function to accept a user_id (forget about the global) and use that parameter to do your lookup/get_option. – murdaugh Oct 4 '13 at 13:21