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 the forum subscription option as part of the personal options on the profile and user edit screen
add_action( 'personal_options', 'forum_subscr_to_profile_form' );
function forum_subscr_to_profile_form($user_obj) {
//get user ID from user object
$user_id = $user_obj->ID;
//get option into variable
$forum_subscr = get_option('forum_subscr');
//check if user is in subscription array
$fs_is = isset($forum_subscr[$user_id]);
//depending on that setup variable for input value
$chkb_val = ( $fs_is == 1 ? 'false' : 'true' );
//depending on that setup variable for checked staus
$chkb_chk = ( $fs_is == 1 ? 'checked="checked"' : '' );
?>
<!-- table row for the additional (small) header -->
<tr class="additional-user-profile-opt">
<th scope="row">
<strong><?php _e('Additional Options'); ?></strong>
</th>
<td></td>
</tr>
<!-- table row for the option: title - checkbox - description -->
<tr class="forum-subscribe">
<th scope="row">
<?php _e('Forum Subscription')?>
</th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span><?php _e('Forum Subscribe') ?></span>
</legend>
<label for="subscr_chkb">
<!-- choose a non-generic name to prevent conflicts, namespacing/prefixing it would be even better -->
<input id="subscr_chkb" type="checkbox" name="forum_subscrbd" value="<?php echo $chkb_val ?>" <?php echo $chkb_chk ?> />
<?php _e('Subscribe to Forum') ?>
</label><br />
</fieldset>
</td>
</tr>
<?php
}
Add/remove data:
//take care of adding and removing the user subscription from the array
add_action( 'personal_options_update', 'forum_subscr_add_remove_user' );
add_action( 'edit_user_profile_update', 'forum_subscr_add_remove_user' );
function forum_subscr_add_remove_user($user_id) {
//get user object
$user_obj = get_userdata($user_id);
//get option into variable
$forum_subscr = get_option('forum_subscr');
if ( !isset($_POST["forum_subscrbd"]) || $_POST["forum_subscrbd"] == false ) {
//remove element from array by
//first setting the value to null
$forum_subscr[$user_id] = null;
//and then filtering with array_filter
$forum_subscr = array_filter($forum_subscr);
update_option( 'forum_subscr', $forum_subscr );
} else {
//add element to array $user_id as key and $user_obj->user_email as value
$forum_subscr[$user_id] = $user_obj->user_email;
update_option( 'forum_subscr', $forum_subscr );
}
}
Keep data up to date:
//make sure the email in the subscription array gets updated when the email gets changed
add_action( 'show_user_profile', 'forum_subscr_keep_email_uptodate' );
add_action( 'edit_user_profile', 'forum_subscr_keep_email_uptodate' );
function forum_subscr_keep_email_uptodate($user_obj) {
$user_obj = get_userdata($user_obj->ID);
$forum_subscr = get_option('forum_subscr');
//update the email for subscribers if the profile page and the subscription array email are not equal
if ( isset($forum_subscr[$user_obj->ID]) && $user_obj->user_email != $forum_subscr[$user_obj->ID] ) {
$forum_subscr[$user_obj->ID] = $user_obj->user_email;
update_option( 'forum_subscr', $forum_subscr );
}
}