Skip Navigation

[Resolved] Send an e-mail when a relationship form is submitted or filter it

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 3 voices.

Last updated by bramV-4 4 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1539121

Tell us what you are trying to do?

I have a site whit a relationship that connects guides and methods. I would like to have an e-mail sent when someone submits the form that connects a guide to a method. The e-mail should either go to the author of the method, or to an e-mail specified in a field of the method. Either way works.

Another option would be that the relationship form was filtered by a filed in the methods, so that only the ones marked was possible to choose.

In one way or another, I need to limit or get a note out about who is connecting to what.

What is the link to your site?
hidden link, you need to be logged in to see the form.

#1539475

Hello, there are a couple of challenges to overcome here because:
1. Email notifications are not currently integrated with relationship forms, only post forms and user forms.
2. There is no public PHP API for relationship form events, similar to cred_save_data and cred_form_validate for posts and users forms.

There is, however, a Post Relationships API that allows you to hook into an event whenever two posts are connected (or disconnected) in a post relationship. Those events are triggered whether the relationship is modified in wp-admin while editing a post, or whether the relationship is modified with a Relationship Form. So you could use these APIs to hook into the relationship event and then use custom code to send an email with wp_mail. These are the post relationships APIs I mentioned:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_before_association_delete

Here is the documentation for wp_mail: https://developer.wordpress.org/reference/functions/wp_mail/

The other solution you mentioned is a custom field filter for the post relationships select field options. I have a code snippet that can be run on the pre_get_posts hook which will help filter the select field options using a meta_query, like any standard WP_Query meta_query conditions. Here is an example:

function ts_only_related_with_field( $query ){

    $parent_post_type_slug = 'SPEAKER';
    $form_id = 1234;

    $meta_query_args = array(
      'key' => 'wpcf-FIELDSLUG',
      'value' => 9,
      'compare' => '=',
      'type' => 'numeric'
    );


    // ----- YOU SHOULD NOT EDIT ANYTHING BELOW THIS LINE ------


    if (defined('DOING_AJAX')) { //We only want to run this if an Actual AJAX Request happens
      if($_REQUEST['action']=='cred_association_form_ajax_role_find'){


       ////We only want to run this if an Actual AJAX Request to the actual cred_association_form_ajax_role_find action happens

            if( $query->query['post_type'][0] == $parent_post_type_slug && $_REQUEST['form_id']==$form_id ) { //if query is for post type and of type PARENT_POST_TYPE, and this is the right form
                $metaquery = array(
                  $meta_query_args
                );
                $query->set( 'meta_query', $metaquery );

            }
        }
    }

}
add_action( 'pre_get_posts', 'ts_only_related_with_field', 101, 1 );

You would change SPEAKER to match the parent post type slug. In a many-to-many relationship it may not be obvious which is the parent and which is the child, so you must edit the relationship to find out. Go to Toolset > Relationships and edit the many-to-many relationship. Click the checkbox to confirm "I understand that changes to these settings may delete post associations in this Relationship" and then "Edit Settings". You'll be able to see which is the parent and which is the child here, but don't edit anything.
Next you will change FIELDSLUG to match the custom field you want to test. My example tests if the custom field value is 9 as a number, but your meta_query arguments may be different. Then you will change 1234 to match the numeric ID of the relationship Form.

Let me know if you have questions about either of these options and I can discuss in more detail.

#1539485

Please note I made some changes in the code just after I published that reply so please be sure to refresh the page and get the latest code.

#1548331

I made a workaround and are waiting for a change in "Email notifications are not currently integrated with relationship forms, only post forms and user forms". Thank you!

#1548361

I made a workaround and are waiting for a change in "Email notifications are not currently integrated with relationship forms, only post forms and user forms"
I strongly encourage you to also submit a request for adding email notifications to relationship forms if you have not already done so:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Any request there will be received and noted by those who are in charge of determining which features to include in upcoming versions of the software. If more people request the same feature, it is more likely to be implemented. Thanks for your consideration.

#1881957

Dear

Is there an idea when email notification for post relationship will be possible.?
Or is there a step by step guide how we can work around this?

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/send-an-e-mail-when-a-relationship-form-is-submitted/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.