• Resolved phloo

    (@phloo)


    Hello, I am trying to find the best way for this case (contact form):

    One reusable form with different recipients:
    – create one contact form in Forminator
    – insert it on different pages/posts
    – set the recipient via custom field/post type or parameter in the shortcode

    Is this possible?

    Details:
    We have different contact persons but the form is always the same and we dont want to create x new forms in Forminator just to be able to change the recipient.

    It is possible to change the value of a hidden field via add_filter. But how do I change the actual mail address for the recipient?

    “forminator_custom_form_mail_user_cc_addresses” is something I found but not really what is needed for this case. Mayb this filter could help: “forminator_get_admin_email_recipients”

    The mail addresses come from a custom field (on each page where the form is insert).
    And I dont want to show them in the source code.

    • This topic was modified 3 years, 6 months ago by phloo.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @phloo,

    Trust you are doing good and thank you for reaching out to this.

    You can configure the email routing to send based on some conditions. If you want to send this email to different recipients conditionally, you can enable the email routing and change the recipients of this email based on the user input. For example, you can add a hidden field and set the default value as Page title or Page ID. Then you can add an email routing with a condition to trigger only when a condition is met (like the hidden field is a particular page title or ID)

    Please find an example of a hidden field: https://ibb.co/kSrzPY7
    Reference for email routing: https://ibb.co/ZTmQGJh

    Please find more about conditions with email notification in our documentation here: https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#email-notifications-forms

    I hope that helps.

    Kind Regards,
    Nebu John

    Thread Starter phloo

    (@phloo)

    hi @wpmudevsupport14

    thanks, but this is not what I asked for. I know about email routing.
    As I tried to explain: the email addresses come from a custom field from the post/page itself.

    It is NOT set in the form itself.

    We want to use 1 global contact form and add the recipient address dynamically.

    Plugin Support Dimitris – WPMU DEV Support

    (@wpmudev-support6)

    Hello there @phloo

    You can achieve this with a little bit of custom coding.

    First, add a Hidden field to your form, select a Custom Value for it and use “custom_email”.

    View post on imgur.com

    Now we will need to replace this default value with the value of the custom field of your pages. Let’s say that your custom field has label “your_custom_email”, then you will need the following filter:

    <?php 
    
    add_filter( 'forminator_field_hidden_field_value', function( $value ){
    
    	if( $value === 'custom_email' ){
    		$post_id = get_queried_object_id();
    		$custom_email = get_post_meta( $post_id, 'your_custom_email', true );
    		$value = $custom_email;
    	}
    	return $value;
    } );

    You can use this in a MU plugin file, ref: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Finally, use the Hidden field value as the actual email recipient:

    View post on imgur.com

    Warm regards,
    Dimitris

    Thread Starter phloo

    (@phloo)

    Hi Dimitris / @wpmudev-support6

    thanks for this guide. Helps a lot.
    I was researching for more solutions and found this:

    add_filter( 'forminator_custom_form_mail_admin_recipients',
      function( $recipients, $custom_form, $data, $entry ){
    	$check_field = 'select-1';
    	$emails = array(
    		'emailone' => array(
    			'[email protected]'
    		),
    		'emailtwo' => array(
    			'[email protected]'
    		),
    		'emailthree' => array(
    			'[email protected]'
    		),
    		'emailfour' => array(
    			'[email protected]'
    		),
    		'emailfive' => array(
    			'[email protected]'
    		),
    		'emailsix' => array(
    			'[email protected]'
    		),
    	);
    
    	$check_value = $data[ $check_field ];
    
    	if ( isset( $emails[ $check_value ] ) ) {
    		return $emails[ $check_value ];
    	}
    
    	return $recipients;
      },
     20, 4 );

    With a little tweaking it will probably work the way without revealing the mail addresses in the html source code.

    Thanks again

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @phloo

    I hope you are doing well

    Thank you for the information, I would suggest editing the code removing the emails as it is a public thread.

    I am marking this case as resolved, but feel free to ping us any time you need.

    Best Regards
    Patrick Freitas

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Form + dynamic recipients from custom field’ is closed to new replies.