Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 }
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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 );
    }
}
share|improve this answer
    
Thanks so much for your answer and clear explanations. I'll test it tomorrow after a bit of sleep. –  mantis Oct 14 '13 at 22:00
    
@mantis Sure, my pleasure! From what I'm seeing, even with the new code, this should get you there. –  ialocin Oct 14 '13 at 22:35
    
Still at it. The problem is that the value in $forum_subscr is not a key value pair of $user_id => $user_email rather a simple array of email addresses. Anyway I'm currently morphing the two codes. –  mantis Oct 15 '13 at 18:59
    
Using the variable $user_id as key for the shouldn't make much difference, besides of course you have to rewrite your code. Just to explain, I used it because you can benefit later on a lot, if you have a key in the array that is assignable, so if you need it, for example to personalize the mails, you don't have to search for the email, which isn't optimal anyways, because you can explicitly determine the user by it's id. @mantis –  ialocin Oct 15 '13 at 19:29
    
I had just figured that out when I read your comment. It seems I have to break things for a few hours before I understand them ;) I added a small edit to keep from having an undefined index but otherwise I didn't have to change a thing. Thanks a million. –  mantis Oct 15 '13 at 20:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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