I have a user profile field which subscribes the user to forum posts by email. This adds the user’s email address to an array in wp-options
. Naturally I want the user to be able to unsubscribe, in which case he would uncheck a checkbox.
Here is how I am trying to do this:
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_users', $user_id ) )
return false;
if(!isset($_POST['subscribed'])){
$useremail = get_userdata($user_id, 'user_email');
$list = (array)get_option('mf_forum_subscribers_1');
$key = array_search($useremail, $list);
unset($list[$key]);
} else {
update_option('mf_forum_subscribers_1', $_POST['subscribed']);
}
}
As it isn’t doing anything (and I am not getting any errors) I assume I’ve done this wrong. I looked for a different hook and found delete_option()
but it only takes one parameter and deletes the whole option.
Also (forgive me if this is an obvious question) in a normal form I would change POST to GET so I could see the values submitted. How can I test this when using hooks? (That’s not the question to be answered I’m just curious.)
Additional information. I realize I probably should have included the functions that show the extra fields in the first place. Here it is :
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<h3>Player information</h3>
<table class="form-table">
<?php if(user_can( $user, 'edit_posts'))
{ ?>
<tr>
<th><label for="team-meta">Email Subscription to Main Forum</label></th>
<td>
<?php if(is_player_subscribed($user->data->user_email)){ ?>
<input type="checkbox" name="subscribe" value="<?php $user->data->user_email ?>" id="subscribed" checked> Automatic Forum e-mails<br>
<?php
} else { ?>
<input type="checkbox" name="subscribe" value="<?php $user->data->user_email ?>" id="subscribed"> Automatic Forum e-mails<br>
<?php } ?>
<span class="description">Check to add subscription to General Forum email</span>
</td>
</tr>
<?php } ?>
</table>
<?php }
There are some problems with your code, below code should answer your question how to delete, remove the subscribers from the options array. I heavily commented the code, which should explain what I did:
Add form:
Add/remove data:
Keep data up to date: