Each user has two user meta values which control whether or not they accept notifications and bulk mail. From the Dashboard->Email Users->User Settings menu make sure the specific user has both settings turned off.
You can do this within a PHP function by updating the user_meta value for each user. The two user meta settings are defined in email-users.php:
// User meta
define( 'MAILUSERS_ACCEPT_NOTIFICATION_USER_META', 'email_users_accept_notifications' );
define( 'MAILUSERS_ACCEPT_MASS_EMAIL_USER_META', 'email_users_accept_mass_emails' );
Set both of these to the string ‘false’ (make sure it is a string) to change a user from receiving email.
I tried the following and it did not work…
function user_role_change_updatePreference( $user_id, $new_role ) {
$site_url = get_bloginfo('wpurl');
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$chk="";
if (empty($new_role)){
update_user_meta($user_id, MAILUSERS_ACCEPT_NOTIFICATION_USER_META, 'false');
update_user_meta($user_id, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META, 'false');
$chk="ran1";
}else
{
update_user_meta($user_id, MAILUSERS_ACCEPT_NOTIFICATION_USER_META, 'true');
update_user_meta($user_id, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META, 'false');
$chk="ran2";
}
$subject = "Role changed: ".$site_url."";
$message = "Hello " .$user_info->display_name . " your role has changed on ".$site_url.", congratulations . $chk . you are now an " . $new_role . " -- AUTO EMAIL FROM email-users/emailusers.php";
wp_mail($to, $subject, $message);
}
add_action( 'set_user_role', 'user_role_change_updatePreference', 10, 2);
as you can see the function also send an email to notify the user about the role change — that email sent & received OK, the variable $chk also show up in the email body as expected depending on which Role the user get changed to… but the user’s “Email Users” email preferences remain the same — on the profile edit page for the user, both checkboxes remain checked after demotion; on the “Email Users->User Settings” page, both Mass Email and Notification remain listed as “on”
I tried putting the above function in the function.php file, then I tried adding it at the end of the email-users.php — same result.
Any suggestions? anything else I should try?
Please advise
In reading through your code it looks correct to me.
Did you verify that the Email Users constants you used are known when this code runs? If possible, I would check the MySQL database to ensure the user meta data for the user id is actually changing.
OK, found the problem… the update_user_meta() calls within the mailusers_any_user_profile_update() function is overwriting mine…
That means I cannot be hooking to ‘set_user_role’ only, need to do what you did and hook the action to ‘personal_options_update’ and ‘profile_update’ instead… the code below is working for me, posting here in case it helps anyone else:
<?php
/**
* Action hook to force un-subscription on users without a role -- at current user profile update
*/
add_action('personal_options_update', 'mailusers_user_emailPreferenceDemotion');
function mailusers_user_emailPreferenceDemotion() {
global $user_ID;
if ( empty( $_POST['role'] ) ){
update_user_meta($user_ID, MAILUSERS_ACCEPT_NOTIFICATION_USER_META, 'false');
update_user_meta($user_ID, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META, 'false');
}
}
/**
* Action hook to force un-subscription on users without a role -- at any user profile update
*/
add_action('profile_update', 'mailusers_edit_user_emailPreferenceDemotion');
function mailusers_edit_user_emailPreferenceDemotion($uid) {
if ( empty( $_POST['role'] ) ){
update_user_meta($uid, MAILUSERS_ACCEPT_NOTIFICATION_USER_META, 'false');
update_user_meta($uid, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META, 'false');
}
}
?>
Mike, please feel free to point out anything you see here that you think may cause problem/conflict with other functions
Thank you much
This looks good to me, marking as resolved.