Forminator Feature ? different recipient depending on selected fields.

Dear all,

I’m looking to find a plug-in that would help me do something a bit specific.

I got a form on my website, where users may select a service (billing, support, etc.).

I’d like the automated e-mail to be sent to a dedicated address (for instance support@domain.com) depending on what the person selected.

Is forminator able to do that ?

If not, would you have a reliable plug-in to suggest ? Or maybe a code snipet to hook into the forminator API ?

Thank you very much for your help.

Regards,

Greg

  • Nithin Ramdas
    • Support Wizard

    Hi Greg,

    Hope you are doing good today. :slight_smile:

    At the moment, I’m afraid, there isn’t any out of the box setting regarding this to send emails based on specific fields selected within a form.

    Our developers are working on a similar feature down the plugins roadmap, to send emails based on conditional choices. At the moment, I’m afraid, we don’t have an exact ETA regarding this, but this feature is something we are looking forward to implement asap.

    This ticket has been brought into our developers attention, and I’m checking with our developer to see whether there is any filters that could be used within the API side. Will keep you updated once I get further feedback regarding this. Have a nice day ahead. :slight_smile:

    Regards,

    Nithin

  • Nithin Ramdas
    • Support Wizard

    Hi Greg,

    I got an update from Panos for a quick workaround, the plugin provides forminator_custom_form_mail_admin_recipients filter, which could be utilised to achieve a similar workflow.

    To implement emails configured in select box, we can take an example of a basic form setup as below:

    [attachments are only viewable by logged-in members]

    [attachments are only viewable by logged-in members]

    You can apply the mentioned filter as:

    <?php

    add_filter( 'forminator_custom_form_mail_admin_recipients', function( $recipients, $custom_form, $data, $entry ){

    $check_field = 'select-1';
    $emails = array(
    'billing' => array(
    'billing@example.com'
    ),
    'support' => array(
    'support@example.com'
    )
    );

    $check_value = $data[ $check_field ];

    if ( isset( $emails[ $check_value ] ) ) {
    return $emails[ $check_value ];
    }

    return $recipients;

    }, 20, 4 );

    You’ll have to make sure the variable entered in the code select-1, ‘billing’ => array, ‘support’ => array are in lower caps, that’s the same as the values entered in the screenshot.

    Once done, the emails would get sent based on the email ID added in the above code, ie the following section:

    $emails = array(
    'billing' => array(
    'billing@example.com'
    ),
    'support' => array(
    'support@example.com'
    )
    );

    You can implement the code as a mu-plugins, please check this manual on how make use of mu-plugins:

    https://wpmudev.com/manuals/wpmu-manual-2/using-mu-plugins/

    If you have any issues with making this work, or not sure how to implement the above, please do let us know, and I’ll make sure to move this ticket into Support Forums, so that we could give a closer look at your website.

    I gave a quick test, and the code works fine in my server, please let us know how that goes. :slight_smile:

    Regards,

    Nithin

    • Greg
      • HummingBird

      Dear Nithin ,

      I’m currently trying to develop this (tests are on-going). I’d also like to create a shortcode that would create a table displaying all past submissions.

      How may I access Forminator submissions from the DB ? Is there a WP_Query or a $wpdb query or some API function available ?

      Regards,

      Greg

      • Greg
        • HummingBird

        Dear Nithin ,

        I was finally able to run some tests.

        On the page (which is private, on a website where I'm not allowed to create new users), the form loads and is submitted as it should.

        [attachments are only viewable by logged-in members]

        The submission worked :

        [attachments are only viewable by logged-in members]

        But, I received the e-mail on the form default address, despite creating a mu-plugin folder with 755 permissions.

        [attachments are only viewable by logged-in members]

        <?php
        add_filter( 'forminator_custom_form_mail_admin_recipients', function( $recipients, $custom_form, $data, $entry ){

        $check_field = 'select-1'; // A mettre à jour
        $emails = array(
        'greg' => array(
        'gregoire@defi-ecologique.com'
        ),
        'greg_gmail' => array(
        'gregoirellorca@gmail.com'
        )
        'pj' => array(
        'pj.crovisier@gmail.com'
        )
        'fanny' => array(
        'fanny.delahalle@alternatiba.eu'
        )
        );

        $check_value = $data[ $check_field ];

        if ( isset( $emails[ $check_value ] ) ) {
        return $emails[ $check_value ];
        }

        return $recipients;

        }, 20, 4 );

        ?>

        1) Could you please help me sort this out ?

        2) How may I access Forminator submissions from the DB ? Is there a WP_Query or a $wpdb query or some API function available ?

        Regards,

        Greg

  • Nithin Ramdas
    • Support Wizard

    Hi Greg,

    Sorry for the delay in getting back to you. I have moved this ticket from members forums to the plugins support forum.

    I suppose you are still looking for a resolution regarding this:

    How may I access Forminator submissions from the DB ? Is there a WP_Query or a $wpdb query or some API function available ?

    I’m afraid, this is a bit complex. And I checked with one of our SLS developers, and he would like to have access to your site and see what fields are used, and the page where it’s implemented to have a better idea to suggest further regarding this.

    Could you please point out which website we should be looking, and the page link, and also please grant support staff access so we could guide you better.

    You can grant access from WPMU DEV > Support > Support Access > Grant Access, or check this manual: https://wpmudev.com/docs/getting-started/getting-support/#chapter-5

    Please let us know once you enable access. Have a nice weekend. :slight_smile:

    Kind Regards,

    Nithin

  • Panos
    • SLS

    Hi Greg !

    Listing the submissions can be tricky as that list in admin page is specifically designed for that page and depends on the field type. I have prepared a sample snippet which you can use as a guide which covers basic fields and upload file url :

    add_shortcode( 'forminator_list_entries', function( $atts, $content ){

    $atts = shortcode_atts(
    array(
    'form_id' => null,
    ), $atts, 'forminator_entries'
    );

    if ( is_null( $atts['form_id'] ) ) {
    return;
    }

    $form = Forminator_API::get_form( (int) $atts['form_id'] );
    $entries = Forminator_API::get_entries( $form->id );
    $_fields = Forminator_API::get_form_wrappers( $form->id );
    $out = '';
    $fields = array();

    if ( empty( $entries ) ) {
    return "There are no entries for form {$form->name}";
    }

    foreach ( $_fields as $field_key => $_field ) {

    if ( isset( $_field['fields'][0]['element_id'] ) ) {
    $fields[] = array( $_field['fields'][0]['element_id'] => $_field['fields'][0]['field_label'] );
    }

    }

    $out = "<table>";

    $out .= "<tr>";

    foreach ( $fields as $key => $field ) {
    $out .= "<td>" .array_values( $field )[0] . "</td>";
    }

    $out .= "</tr>";

    foreach ( $entries as $key => $entry ) {

    $meta_data = $entry->meta_data;
    $out .= "<tr>";

    foreach ( $meta_data as $meta_key => $data ) {
    $value = isset( $data['value'] ) ? $data['value'] : '';

    if ( is_array( $value ) ) {
    if ( isset( $value['file']['file_url'] ) ) {
    $value = $value['file']['file_url'];
    }
    }

    $out .= "<td>{$value}</td>";

    }

    $out .= "<tr>";

    }

    $out .= "</table>";

    return $out;

    } );

    You can use the [forminator_list_entries] shortcode with the form_id att, eg [forminator_list_entries form_id=”1468″]

    Hope this helps!

    Kind regards!

    • Greg
      • HummingBird

      Dear Panos ,

      Thank you for your answer.

      I've tried that, but I can actually notice that my MU plug-in isn't working.

      [attachments are only viewable by logged-in members]

      It might also explain why the first code snippet isn't triggered either.

      I've created the following file : /wp-content/mu-plugin/forminator_anv.php

      <?php
      /**
      * Plugin Name: Forminator : Ajustements pour ANV
      * Plugin URI: https://anv-cop21.org/test-formulaire-de-contact/
      * Description: Utilisé pour modifier le fonctionnement naturel de Forminator Pro
      * Author: Grégoire Llorca - gregoire@defi-ecologique.com
      */

      // Envoyer un mail à un destinataire différent selon le champ saisi
      add_filter( 'forminator_custom_form_mail_admin_recipients', function( $recipients, $custom_form, $data, $entry ){

      $check_field = 'select-1'; // A mettre à jour
      $emails = array(
      'greg' => array(
      'gregoire@defi-ecologique.com'
      ),
      'greg_gmail' => array(
      'gregoirellorca@gmail.com'
      ),
      'pj' => array(
      'pj.crovisier@gmail.com'
      ),
      'fanny' => array(
      'fanny.delahalle@alternatiba.eu'
      )
      );

      $check_value = $data[ $check_field ];

      if ( isset( $emails[ $check_value ] ) ) {
      return $emails[ $check_value ];
      }

      return $recipients;

      }, 20, 4 );

      // Afficher l'historique des contacts
      add_shortcode( 'forminator_list_entries', function( $atts, $content ){

      $atts = shortcode_atts(
      array(
      'form_id' => null,
      ), $atts, 'forminator_entries'
      );

      if ( is_null( $atts['form_id'] ) ) {
      return;
      }

      $form = Forminator_API::get_form( (int) $atts['form_id'] );
      $entries = Forminator_API::get_entries( $form->id );
      $_fields = Forminator_API::get_form_wrappers( $form->id );
      $out = '';
      $fields = array();

      if ( empty( $entries ) ) {
      return "There are no entries for form {$form->name}";
      }

      foreach ( $_fields as $field_key => $_field ) {

      if ( isset( $_field['fields'][0]['element_id'] ) ) {
      $fields[] = array( $_field['fields'][0]['element_id'] => $_field['fields'][0]['field_label'] );
      }

      }

      $out = "<table>";

      $out .= "<tr>";

      foreach ( $fields as $key => $field ) {
      $out .= "<td>" .array_values( $field )[0] . "</td>";
      }

      $out .= "</tr>";

      foreach ( $entries as $key => $entry ) {

      $meta_data = $entry->meta_data;
      $out .= "<tr>";

      foreach ( $meta_data as $meta_key => $data ) {
      $value = isset( $data['value'] ) ? $data['value'] : '';

      if ( is_array( $value ) ) {
      if ( isset( $value['file']['file_url'] ) ) {
      $value = $value['file']['file_url'];
      }
      }

      $out .= "<td>{$value}</td>";

      }

      $out .= "<tr>";

      }

      $out .= "</table>";

      return $out;

      } );

      ?>

      Do you see any reason why it doesn't work ? Access rights for both folder and file is "755" : https://wpmudev.com/manuals/wpmu-manual-2/using-mu-plugins/

      Regards,

      Greg

  • Ash
    • Code Norris

    Hello Greg

    The folder name should be mu-plugins, not mu-plugin (it must have a “s” at the end, the plural form) :slight_smile:

    So the path would be /wp-content/mu-plugins/forminator_anv.php

    And then, in the shortcode, I can see you used [forminator_list_entries form_id=>>1468″] but that should be like this: [forminator_list_entries form_id=”1468″]

    Please correct those and it should be okay :slight_smile:

    Have a nice day!

    Cheers,

    Ash

    • Greg
      • HummingBird

      Dear Ash ,

      Thank you for your help.

      I’ve been able to sort this out, using the right filename sorted the formatting issue.

      Now, I can display submissions as I want, which is swell, cheers.

      However, sending e-mails doesn’t work, since I tweaked the code you sent me.

      // Correspondance des e-mails
      function get_emails(){
      $emails = array(
      'greg' => array(
      'gregoire@defi-ecologique.com',
      "Envoi mail à Grégoire Llorca"
      ),
      'greg_gmail' => array(
      'gregoirellorca@gmail.com',
      "Envoi mail à Grégoire Llorca (gmail)"
      ),
      'pj' => array(
      'pj.crovisier@gmail.com',
      "Envoi mail à PJ"
      ),
      'fanny' => array(
      'fanny.delahalle@alternatiba.eu',
      "Envoi mail à Fanny"
      )
      );
      return $emails;
      }

      // Envoyer un mail à un destinataire différent selon le champ saisi
      add_filter( 'forminator_custom_form_mail_admin_recipients', function( $recipients, $custom_form, $data, $entry ){

      $check_field = 'select-1'; // A mettre à jour
      $emails = get_emails();
      $check_value = $data[ $check_field ];

      if ( isset( $emails[ $check_value ] ) ) {
      return $emails[ $check_value ];
      }

      return $recipients;

      }, 20, 4 );

      I tried to add some var_dump here and there, but there is nothing displayed on the page.

      Regards,

      Greg