0

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 that is_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
0

This is an expansion of my comment on OP:

function is_forum_subscribed($user_ID) {
  if($user_ID)
  {
    $useremail = $this->get_userdata($user_ID, 'user_email');
    $list = get_option("mf_forum_subscribers_".$this->current_forum, array());
    if(in_array($useremail, $list))
      return true;
    }
    return false;
  }
}

You can call this function like this, instead:

is_forum_subscribed($player->ID);
  • Thanks @murdaugh I created two separate functions instead, as the other two originally dealt with the current user and I want a function that runs on init and covers all users to whom it applies. I'm having trouble calling it or hooking into it though. I tried `add_action( 'init', "forum_subscribe_member_player" ) but perhaps I'm calling it in the wrong place. – mantis Oct 4 '13 at 16:19
  • If init is too early, you might try calling it in wp_loaded but it's hard to know without seeing all the resources you're trying to utilize. – murdaugh Oct 4 '13 at 18:59
  • Thanks, I'll try that. Its all contained in a class that does have an init function but everything refers to the class (using $this). Would it make more sense to include it in functions.php since it deals only with the options table and doesn't refer to anything in the class(that I can see?) Or is that a bad idea? – mantis Oct 5 '13 at 9:09
  • If you can, toss that entire class (with your is_forum_subscribed() function) in a gist. It would be a lot easier to understand what is happening that way. – murdaugh Oct 5 '13 at 14:09
  • I actually decided it belonged in functions.php (or perhaps I was just being lazy) but I still need to figure out how to exclude those who have clicked unsubscribe if($this->is_forum_subscribed()) but that may be another question. Here's th gist in case you're curious: gist.github.com/mantismamita/6845919 Thanks for your help. – mantis Oct 6 '13 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.