0

I have an option that subscribes members to forum email notifications. The list of emails is in an array as an option value. All is working fine and all subscribing and unsubscribing is done using update_option();

My problem is that were a user to be deleted it would be difficult to unsubscribe him without going into the database. So I thought this function would do it but I must be missing something :

add_action( 'delete_user', 'forum_remove_deleted_user' );

function forum_remove_deleted_user($user_id)
{   
    $user_obj = get_userdata($user_id); 

    $subscbr_email = $user_obj->user_email;

    if(!empty($subscbr_email)) {                    
        $list = (array)get_option('mf_forum_subscribers_1');

        if(!empty($list) &&  is_subscribed($subscbr_email)) {
            $key = array_search($subscbr_email, $list);
            unset($list[$key]);
        }               
    }       
}

function is_subscribed($subscbr_email)
{
    if($subscbr_email )
      {
        $list = (array)get_option('mf_forum_subscribers_1');
        if(in_array($subscbr_email , $list)) {
          return true;
        }
        return false;
      }     
}

I'd love to know what I'm doing wrong. Otherwise I suppose I could use $wpdb or perhaps I should?

4
  • Have you done any debugging? Any idea where in that code it goes wrong?
    – s_ha_dum
    Oct 26, 2013 at 21:52
  • I've used the code elsewhere in the project (before I came up with another way to do things) and everything went as expected.
    – mantis
    Oct 26, 2013 at 22:46
  • You didn't even attempt to answer the questions asked, but @G-M may have spotted the problem.
    – s_ha_dum
    Oct 26, 2013 at 22:49
  • I did var_dump or echo everything to answer your question. Thanks for having a look.
    – mantis
    Oct 26, 2013 at 23:05

1 Answer 1

0

If what posted is all the code you are using, you unsetting the value from the retreved array using unset, but then you don't save again the array on database:

add_action( 'delete_user', 'forum_remove_deleted_user' );

function forum_remove_deleted_user($user_id) {   
    $user_obj = get_userdata($user_id); 

    $subscbr_email = $user_obj->user_email;

    if( ! empty( $subscbr_email ) ) {                    
        $list = (array) get_option('mf_forum_subscribers_1');

        if( ! empty($list) && ( false !== array_search($subscbr_email, $list) ) ) {

            $key = array_search( $subscbr_email, $list );
            unset( $list[$key] );

            update_option('mf_forum_subscribers_1', $list); // you miss this part
        }               
    }       
}

For sake of semplicity I removed the dependency from is_subscribed function that is unneeded here.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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