Load Ultimate Member dynamic email in Contact Form 7 send email

609 views Asked by At

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.

1

There are 1 answers

3
Howard E On BEST ANSWER

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.

function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
    if ( $submission && 799 === $contact_form->id() ) {
        $posted_data = $submission->get_posted_data();
        if ( isset( $posted_data['um-profile-id'] ) ) {
            $user                            = get_user_by( 'id', absint( $posted_data['um-profile-id'] ) );
            $properties                      = $contact_form->get_properties();
            $properties['mail']['recipient'] = $user->user_email;
            $contact_form->set_properties( $properties );
        }
    }
}
add_action( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );


add_action( 'wpcf7_init', 'um_profile_id_form_tag' );
function um_profile_id_form_tag() {
    wpcf7_add_form_tag( 'um_profile_id', 'cf7_get_um_profile_id' );
}

function cf7_get_um_profile_id() {
    $output = sprintf( '<input type="hidden" name="um-profile-id" value="%s"/>', um_profile_id() );
    return $output;
}

This method has been tested and works.