I am using the Ultimate Member plugin and I need CF7 to load the email of each user for sending that are within the public profiles that are the members.
Here I leave the code that I have used but it only works for me if I put an email in string format, $dynamic_email = '[email protected]';
instead of $dynamic_email = $value;
How do I make the following dynamic code work for me?
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
$form_id = $contact_form->id();
if($form_id == 799 ){
$valor = get_the_author_meta('user_email', um_profile_id()); // um_profile_id() - Profile ID in the Ultimate Member plugin
$dynamic_email = $valor;
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $dynamic_email;
$contact_form->set_properties($properties);
return $contact_form;
}
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
I need to load the value of get_the_author_meta('user_email', um_profile_id())
so that I upload the mail in each of the public profiles.
Thank you very much for the help.
Summarizing this function if it works, it manages to change the sending email and sends the form:
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
$form_id = $contact_form->id();
if($form_id == 799 ){
$dynamic_email = '[email protected]';
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $dynamic_email;
$contact_form->set_properties($properties);
return $contact_form;
}
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
This other one that is the one I need does not work and does not send the mail showing the error "An error occurred while trying to send your message. Please try again later." and I do not understand or know the reason why it does not work if I only make a small change and it also returns an email:
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
$form_id = $contact_form->id();
if($form_id == 799 ){
$valor = get_the_author_meta('user_email', um_profile_id()); // um_profile_id() - Profile ID in the Ultimate Member plugin
$dynamic_email = $valor;
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $dynamic_email;
$contact_form->set_properties($properties);
return $contact_form;
}
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
Thanks for your help.
To pass the
um_profile_id()
to the form data, you need to add it to the form (as stated in the comments by myself and @cbroe). To do that, either you need to pass it using javascript to a hidden form field, or simply create a custom form tag. Below uses a custom form tag.Add
[um_profile_id]
to your form, and the below function will work.Additionally, since wpcf7_before_send_mail is an action hook you don't have to return anything, the $contact_form object is passed by reference and updated when you set the property.
This method has been tested and works.