Hi,
I created a custom user field "receive_notification" where the user can set "yes" if he wants to receive notifications and "no" otherwise.
Now, in a CRED POST form that create an advertisement, I want to send an email notification to all users who have set "receive_notification" to "yes" in their profiles.
https://toolset.com/forums/topic/send-email-notification-to-user/ solved a problem similar to mine but I do not want to show the generic field and I have a custom user field instead of "role". Is it the right way to solve my problem? Shall I still use the generic field but hide it?
add_shortcode('get-all-users', 'get_all_users_data');
function get_all_users_data($attr) {
global $post;
if(isset($attr['role']) and $attr['role']!='')
$role = $attr['role'];
else
$role = 'administrator';
$blogusers = get_users( 'orderby=user_id&role='.$role);
$temp = array();
foreach ( $blogusers as $user ) {
$temp[]= '{"value":"'.$user->ID.'","label":"'.$user->user_email.'"}';
}
$temp = join(',',$temp);
$str = '['.$temp.']';
return $str;
}
Your help is appreciated.
Dear Nabils,
In your case, I suggest you try with CRED filter hook "cred_notification_recipients", it does not need any generic field,
For example:
The custom user field "receive_notification" is created with Types plugin, in database the field meta_key is "wpcf-receive_notification", when user enabled the field, it stores value "1" into database, then you can modify the PHP codes as below:
add_filter('cred_notification_recipients', 'send_subscribed_users_func', 10, 4);
function send_subscribed_users_func($recipients, $notification, $form_id, $post_id){
if ( 123 == $form_id){ //if it is specific CRED form
$args = array(
'meta_key'=> 'wpcf-receive_notification',
'meta_value' => 1,
'fields' => array( 'display_name', 'user_email')
);
//get all users with "receive_notification" field
$users = get_users( $args );
//add them as recipients
foreach($users as $user){
$recipients[] = array(
'address' => $user->user_email,
'name' => $user->display_name
);
}
}
return $recipients;
}
Please replace 123 with your CRED form ID.
More help:
https://developer.wordpress.org/reference/functions/get_users/
Dear Luo,
I tried this but did not get any email.
Log error shows:
PHP Notice: PHP Notice: Undefined index: to in /home1/capitbq3/public_html/tadreeboman/wp-content/plugins/cred-frontend-editor/embedded/classes/Notification_Manager.php on line 1110
[24-May-2016 08:16:50 UTC] PHP Notice: Undefined index: lastname in /home1/capitbq3/public_html/tadreeboman/wp-content/plugins/cred-frontend-editor/embedded/classes/Notification_Manager.php on line 1121
Actually, the code will define the recipients, but we have also to define the notification in the CRED where we have to define the subject, body... I tried this but I am not sure which option I should set for "Where to send this notification:"? Send notification to a specific email address:?
Are you going to do this?
Send the Notification email to the wordpress users, whose user filed "receive_notification" is set, and different subject and body for each user, for example:
To user 1, subject 1, body 1
To user 2, subject 2, body 2
...
This is not possible within CRED form, I suggest you check out our certified partners for it:
https://toolset.com/consultant/
If you only need send same email to wordpress users, whose user filed "receive_notification" is set,
You can modify the PHP codes as below:
add_filter('cred_notification_recipients', 'send_subscribed_users_func', 10, 4);
function send_subscribed_users_func($recipients, $notification, $form_id, $post_id){
if ( 123 == $form_id){ //if it is specific CRED form
$args = array(
'meta_key'=> 'wpcf-receive_notification',
'meta_value' => 1,
'fields' => array( 'display_name', 'user_email')
);
//get all users with "receive_notification" field
$users = get_users( $args );
//add them as recipients
foreach($users as $user){
$recipients[] = array(
'to' => 'to',
'address' => $user->user_email,
'display_name' => $user->name
);
}
}
return $recipients;
}
Please replace 123 with your CRED form ID.
Thank you Luo,
Your help is most appreciated.
Actually, I want to send the same email to all users. I tried your code and it works, thank you. However, I need "receive_notification" to be checkboxes (not a select) and the email should be sent if the user has checked option "option1" of the checkboxes type. I tried to update your code, but this checkoxes is very tricky and could not find a similar example in the net.
HI
Sorry, maybe no need to bother with this checkboxes (you can post the solution for other users if you want). I used multiple instances of the checkbox type instead and it does the job.
Thank you
OK, please let me know if you need other assistance. thanks