我有一个用户配置文件字段,它通过电子邮件订阅用户论坛帖子。这会将用户的电子邮件地址添加到wp-options
. 当然,我希望用户能够取消订阅,在这种情况下,他会取消选中复选框。
下面是我如何做到这一点的:
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\']);
}
}
由于它没有做任何事情(我也没有收到任何错误),我认为我做错了。我找了一个不同的钩子
delete_option()
但它只接受一个参数并删除整个选项。
此外(如果这是一个明显的问题,请原谅我),我会以正常的形式更改帖子,以便查看提交的值。在使用挂钩时,如何测试这一点?(这不是要回答的问题,我只是好奇。)
其他信息。我意识到我可能应该首先包含显示额外字段的函数。这是:
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 }
最合适的回答,由SO网友:Nicolai Grossherr 整理而成
您的代码有一些问题,下面的代码应该可以回答您的问题,即如何从选项数组中删除订阅服务器。我对代码进行了大量注释,这应该可以解释我所做的:
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 );
}
}