Skip Navigation

[Resolved] Post form e-mail notification recipients question.

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 17 replies, has 2 voices.

Last updated by lesleeM 1 year, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2606723

Is it possible to direct the e-mail notification on a post form as outlined below:

1) We have a Rides Content type that has a relationship with a Ride Leader content type.

2) Each Ride has at least one Ride Leader assigned to ii.

3) The Ride Leader content type has E-Mail address as one of the fields in its custom field group.

4) The Post Form is present on each Ride page and each Ride page can have a different Ride Leader.

5) We would like the notifications for the post form to go to the Ride Leader chosen on the ride page where the end-user is submitting the form.

Is that possible? Thanks for your assistance.

#2606785

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Yes, I think its possible. Based on the information you shared it seems you want to send email notification to parent post email - is that correct? if yes:

Can you please tell me where I can see the form to submit and admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2606987

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please tell me where I can see the following form on the frontend:
- hidden link

You have one to many relationship between Ride and Registrations so one Ride can have many registrations.

You have another relationship between Rides and Ride Leaders and this is many-to-many relationship.

You have the form created that creates the new Registration and we can set using the form that the current registration is for what Ride as there is one to many post relationship between Ride and Registrations.

So, lets assume we try to create new entry using the following form:
- hidden link

But using the form as I already shared we can connect Rieds to new registration but how to get Ride Leader email when creating new registration as we do not have any information about the Ride Leader.
- Is the Ride leader already connected to Ride post when we create new registration and connect specific ride to it?

As there is many-to-many relationship between Rides and Ride Leaders, so we connect Ride XX to new registration and based on this Ride XX we will try to get all related Ride Leaders but it will be multiple as its many-to-many post relationship.

So you want to send emails to all Ride leaders for the Ride XX that is selected for registration - is that correct?

#2607007

Answers:

1) Is the Ride leader already connected to Ride post when we create new registration and connect specific ride to it?

The form is on any Ride post. We're using this one to test on.

hidden link

2) Is the Ride leader already connected to Ride post when we create new registration and connect specific ride to it?

For all Rides that are in the future, we will have the Ride Leader relationship already established before anyone will submit the form. The Ride Leader relationship is established as the new Ride posts are created. We do have many Rides in our system that do not have the Ride Leader relationship established though. But those are all past rides that we now have in the system as an archive for the purposes of building our Ride Maps page. Whenever past rides will become future rides again, the Ride Leader relationship will be set for 100% of them at that point, so that information will be available for the Registration form to pull from.

3) So you want to send emails to all Ride leaders for the Ride XX that is selected for registration - is that correct?

We have the Ride Leader to Ride relationship set as many-to-many because each ride can have up to two ride leaders. I think we have it limited to a maximum of two. Most of the Rides only have one. But some have two. There are two Ride Leader sub-types that are separate from the relationship. One is the Ride Contact which is the e-mail address we want to send to. The other less used one is Ride Sweep. If it is easier to have the form send Registration notifications to both potential Ride Leaders, that's fine. But if it is easier to only send to one, just have it go to the one defined as Ride Contact. That's more complicated setup that Waqar helped us with though, so that may not be something that can be done easily since the Ride Contact and Ride Sweep fields are separately defined from the Ride Leader relationship with Rides. Those two fields are custom select fields set up to pull all the Ride Leader post titles as their options, so I don't know if you could easily determine a Ride Contact e-mail address through that. We set up both of those as needed AND set up the Ride Leader relationship when we publish a new Ride. It's a little bit redundant on the admin side to do it twice like that, but it gets us all the functionality we've tried to achieve here, so I don't argue with success. I wouldn't want to restructure everything further to make that easier because everything else is working. For this, I'm thinking sending to both potential Ride Leaders will likely be the easier approach given how I know everything is structurally set up. But I'll let you decide that from how you see everything is currently set up. Go with whatever is easier for you now.

#2607127

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I can see there is hook already added at the "Custom Code" section with the code snippet "notification-recipients":

Can you tell me for what purpose you are using the following hook?

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
 
    // Check notification name matches target notification
    if ( $form_id == 2327 && isset($notification['name']) && 'Ride Notification' == $notification['name'] ) {
      	$ride_id = toolset_get_related_post($post_id, 'ride-notification');
      	$registration_posts = toolset_get_related_posts( 
            // get posts related to this one
            $ride_id, 

            // Relationship between the posts
            'ride-registration', 

            // Additional arguments
            [
                // Get posts where $writer is the parent in given relationship.
                // This is mandatory because we're passing just a single $writer post as the first parameter.
                'query_by_role' => 'parent', 
                'role_to_return' => 'child',
                'return' => 'post_id'
            ]
        );
      	foreach($registration_posts as $registration_id){
        	
          	$address = get_post_meta($registration_id, 'wpcf-email', true);
          	$name = get_post_meta($registration_id, 'wpcf-name', true);
          	if(!$address){
              	continue;
            }
            // Add a BCC to registration email
            $recipients[] = array(
                'to'        =>  'bcc',
                'address'   => $address,
                'name'      =>  $name,
                );
        }
    }
          
    return $recipients;
}
#2607185

That's a totally separate bit of functionality for another Relationship based custom content type called Notifications. That should not be confused with the post form notifications we're looking at here.

So you have full understanding, we have the Ride Registration form on the Ride page. People will register for the Ride. We then have a View showing everyone that registers for the ride.

Below that, we then have a Ride Notification form on the Ride page below that which is only accessible to people with Admin or Ride Leader role assignments. I still have to double-check that I have that role based conditional set up correctly, but I think I do. The purpose of this Notifications post form is so Ride Leaders or Admins can send Notification messages directly to everyone that registers for a Ride. The e-mail recipient list for Notifications is pulled from the Registrations by the code you found here.

Say, for example, we have a ride scheduled to start at 8:00am. But then the day before the ride, the Ride Leader sees a weather forecast showing a high chance of stormy weather at that time and decides to change the start time to a later time to avoid the bad weather. We wanted a system where Ride Notifications could be sent to announce any late changes to the information about the ride that would e-mail that to all registered riders.

Then lastly we have another View showing the content of all Ride Notifications below where the form is placed. That is not set to conditionally display. Everyone should see the notifications that get added (if any). Everything with Ride Notifications was working fine when I last tested it, but I'm going to be going through all of that one more time too, now that I'm seeing these different issues with the Registrations. That testing step for Notifications will come after we hopefully get Registrations exactly right.

I'm getting the Board of Directors of the club to test all of this out so I can see how someone does with it without any instructions and to make sure everything is working for them. When I have them test, I'm not giving them any instructions beyond saying "Go to a ride page and try the registration system". I want to make sure the system is very easy and intuitive to use. Many of the people on the Board of the club are also Ride Leaders, so they will be the ones using this system extensively. If they can succeed without instruction, then I think our end-users will do fine with it as well. I'm testing their reactions to it now to determine if I'll need to add instructions for end-users or not. But I thought everything was working previously and got this disappointment when I saw things that are broken that were working previously. So I told them to hold off on further testing until I work through these new issues or newly realized issues with you. We're still close.

#2607563

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I would like to know what email notification you want to use to send the email to all Ride leaders for the Ride XX that is selected for registration?

Should I use the same email notification "Ride Registration Notification" available with the following registration form?
- hidden link

#2607683

Correct. That's the one where I have the code you supplied.

[types field='name' item="%%POST_ID%%"][/types] has registered for your %%PARENT_POST_TITLE%% ride.

#2607873

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjusted the code added to "Custom Code" section with the code snippet "notification-recipients":
=> hidden link

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
 
    if ( $form_id == 2327 && isset($notification['name']) && 'Ride Notification' == $notification['name'] ) {
      	$ride_id = toolset_get_related_post($post_id, 'ride-notification');
      	$registration_posts = toolset_get_related_posts( 
            // get posts related to this one
            $ride_id, 

            // Relationship between the posts
            'ride-registration', 

            // Additional arguments
            [
                // Get posts where $writer is the parent in given relationship.
                // This is mandatory because we're passing just a single $writer post as the first parameter.
                'query_by_role' => 'parent', 
                'role_to_return' => 'child',
                'return' => 'post_id'
            ]
        );
      	foreach($registration_posts as $registration_id){
        	
          	$address = get_post_meta($registration_id, 'wpcf-email', true);
          	$name = get_post_meta($registration_id, 'wpcf-name', true);
          	if(!$address){
              	continue;
            }
            // Add a BCC to registration email
            $recipients[] = array(
                'to'        =>  'bcc',
                'address'   => $address,
                'name'      =>  $name,
                );
        }
    }

  if ( $form_id == 2083 && isset($notification['name']) && 'Ride Registration Notification' == $notification['name'] ) {
		
		$ride_id = toolset_get_related_post($post_id, 'ride-registration');
      	$ride_leader_posts = toolset_get_related_posts( 
            // get posts related to this one
            $ride_id, 

            // Relationship between the posts
            'ride-ride-leader', 

            // Additional arguments
            [
                // Get posts where $writer is the parent in given relationship.
                // This is mandatory because we're passing just a single $writer post as the first parameter.
                'query_by_role' => 'parent', 
                'role_to_return' => 'child',
                'return' => 'post_id'
            ]
        );
      	foreach($ride_leader_posts as $ride_leader_id){
        	
          	$address = get_post_meta($ride_leader_id, 'wpcf-ride-contact-email', true);
          	if(!$address){
              	continue;
            }
            // Add a BCC to registration email
            $recipients[] = array(
                'to'        =>  'to',
                'address'   => $address,
                
                );
        }
		
	}
          
    return $recipients;
}

The code I've added to the above hook is as given under for the form ID 2083 that is used for registration:

  if ( $form_id == 2083 && isset($notification['name']) && 'Ride Registration Notification' == $notification['name'] ) {
		
		$ride_id = toolset_get_related_post($post_id, 'ride-registration');
      	$ride_leader_posts = toolset_get_related_posts( 
            // get posts related to this one
            $ride_id, 

            // Relationship between the posts
            'ride-ride-leader', 

            // Additional arguments
            [
                // Get posts where $writer is the parent in given relationship.
                // This is mandatory because we're passing just a single $writer post as the first parameter.
                'query_by_role' => 'parent', 
                'role_to_return' => 'child',
                'return' => 'post_id'
            ]
        );
      	foreach($ride_leader_posts as $ride_leader_id){
        	
          	$address = get_post_meta($ride_leader_id, 'wpcf-ride-contact-email', true);
          	if(!$address){
              	continue;
            }
            // Add a BCC to registration email
            $recipients[] = array(
                'to'        =>  'to',
                'address'   => $address,
                
                );
        }
		
	}
#2607899

I don't think this worked. The notification e-mail I received did not go to the Ride Leader e-mail address. It went to the e-mail address of my admin user account that I used to submit the Registration. I'm seeing on the Registration post form that the registration notification is set to:

Send notification to the post author

That's why it is going to the address of the user account used to submit the form. Does that setting need to be changed to for your code to work to send to the Ride Leader e-mail address?

Actually, looking more at your code, am I understanding it correctly in seeing that it is adding sending to the Ride Leader as a BCC? If so, I guess that would be fine to have it go to both the person submitting the form and the ride leader if that's easier for you to do it that way. I was thinking just send it to the Ride Leader only, but it's OK to leave it as is. But my Ride Leader e-mail address on this Test Ride did not get the notification about the Registration.

Sorry this is this complicated and huge thanks for the effort for us here.

#2608385

After I did the guest user Registration test I showed you in the other thread, I did another one for the Test Ride here.

hidden link

That test worked as well as far as the relationship problem, so I think we're good on that now.

This is a ride where I set myself up as the Ride Leader, so please use this Ride for all tests of the e-mail notification feature. I'll get the e-mail sent to my km77@cfl.rr.com address if this is working. If we use other rides, chances are the notifications will go to other Ride Leaders and I'd have to ask them if they got anything. Using this Test Ride keeps it to where I'll get everything.

The e-mail notification did not come to either the user e-mail address or the ride leader e-mail address. So that's the only thing not working now. I believe the issue with the Ride Name field not showing in the notification when guest users registered will be fixed now that the relationship is working between guest user Registrations and Rides. So we should be good there. All that remains is getting the post form submission notification sent to the Ride Leader e-mail address.

Note that I checked the Post SMTP log for if the e-mail notifications went out and here's what I see.

hidden link

I'm not seeing anything listed on May 20, so I don't think the notification went out at all this time. Previously it was sending to the person that submitted the Registration.

As for getting the correct e-mail address to send to the Ride Leader for each ride, something I'm seeing in the relationship has me worried that this is going to still be difficult to achieve. For that concern, go here:

hidden link

Scroll down to near the bottom and see the Ride Registration in there. Note that the Ride Contact field is not populated. I hope that doesn't make it harder to pull the Ride Leader e-mail address field. It is not problematic to us that the Ride Contact field isn't populated here, so don't worry about that if it has no impact on this effort to pull the Ride Leader e-mail address. I just wanted to point it out to you in case it does have an impact here.

If you go to the Ride edit page for that Test Ride here:

hidden link

You'll see the Ride Registrations are all in there correctly now, including my 5/20/23 one. The Ride Leader relationship to the Ride is working as well and the phone number field is populated there. I'm hoping you can easily pull the phone field from this.

Let me know if I need to establish another relationship between Ride Registration and Ride Leader in some way for this to work. I'm still not sure if two separate child posts related to the same parent post can pull from each other even if they are not directly related. So I'm thinking we might need to add a relationship between Ride Registration and Ride Leader. I could be entirely wrong in that thought.

#2608715

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The admin access details is not working now at this end.

Can you please send me working admin access details so I can dive in to the backend and check the email log.

I have set the next reply to private which means only you and I have access to it.

#2608855

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now:
- hidden link

I can see now the email send to the email ID km77@cfl.rr.com on the above email log.

I've removed all the hidden fields from the form even I've removed the field that you tried to hide by adding the custom HTML block from your form:
=> hidden link

So the final version of the code for code snippet "registration-title" is as given under:

add_action('cred_save_data', function($post_id, $form_data) {
  
  if ($form_data['id']==2083) { // speccific post form ID

    $name = get_post_meta($post_id, 'wpcf-name', true);
    $email = get_post_meta($post_id, 'wpcf-email', true);
    $date = get_the_date('m/d/Y', $post_id);
   
    /// getting parent ride id
    $parent_ride_id = $_POST['_cred_cred_prefix_cred_container_id'];
    $parent_ride_title = get_the_title($parent_ride_id);
      
    /// connecting the posts
    toolset_connect_posts('ride-registration',$parent_ride_id,$post_id);
    
$post_title = "[".$parent_ride_title. "] Registration - [".$name."] [".$date."]";

 ///  $post_title = sprintf('[%1$s] Registration - [%2$s] [%3$s]', $parent_ride_title, $name, $date);
    
  $slug = sanitize_title($post_title);
    wp_update_post(array(
      'ID'=>$post_id, 
      'post_title'=>$post_title,
      'post_name' => $slug
    ));
  }
},10, 2);

And for the code snippet "notification-recipients" code is adjusted as given under:

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
 
    // Check notification name matches target notification
    if ( $form_id == 2327 && isset($notification['name']) && 'Ride Notification' == $notification['name'] ) {
      	$ride_id = toolset_get_related_post($post_id, 'ride-notification');
      	$registration_posts = toolset_get_related_posts( 
            // get posts related to this one
            $ride_id, 

            // Relationship between the posts
            'ride-registration', 

            // Additional arguments
            [
                // Get posts where $writer is the parent in given relationship.
                // This is mandatory because we're passing just a single $writer post as the first parameter.
                'query_by_role' => 'parent', 
                'role_to_return' => 'child',
                'return' => 'post_id'
            ]
        );
      	foreach($registration_posts as $registration_id){
        	
          	$address = get_post_meta($registration_id, 'wpcf-email', true);
          	$name = get_post_meta($registration_id, 'wpcf-name', true);
          	if(!$address){
              	continue;
            }
            // Add a BCC to registration email
            $recipients[] = array(
                'to'        =>  'bcc',
                'address'   => $address,
                'name'      =>  $name,
                );
        }
    }

if ( $form_id == 2083 && isset($notification['name']) && 'Ride Registration Notification' == $notification['name'] ) {
      
	  // getting parent ride ID
      $ride_id = $_POST['_cred_cred_prefix_cred_container_id'];
	  
      $ride_leader_posts = toolset_get_related_posts( 
          // get posts related to this one
          $ride_id, 
 
          // Relationship between the posts
          'ride-ride-leader', 
 
          // Additional arguments
          [
              // Get posts where $writer is the parent in given relationship.
              // This is mandatory because we're passing just a single $writer post as the first parameter.
              'query_by_role' => 'parent', 
              'role_to_return' => 'child',
              'return' => 'post_id'
          ]
      );
      foreach($ride_leader_posts as $ride_leader_id){
           
          $address = get_post_meta($ride_leader_id, 'wpcf-ride-contact-email', true);
          if(!$address){
              continue;
          }
          // Add a BCC to registration email
          $recipients[] = array(
              'to'        =>  'to',
              'address'   => $address,
               
              );
      }
       
  }


          
    return $recipients;
}

Can you please confirm it works as expected now.

#2608885

Confirmed that the post form notification for Registration went to the correct Ride Leader address, so this ticket is now working perfectly. But now the Ride title functionality has broken again in the notification itself (or wasn't fixed). The message I received from the post form notification this time is:

Minesh Shah has registered for your ride.

It should have said

Minesh Shah has registered for your Test Ride ride.

That goes back to this ticket:

https://toolset.com/forums/topic/post-form-notification-e-mail-not-showing-toolset-fields/

I don't know if you did this test Registration as a logged in user or as a Guest user, but my previous thought that if the Relationship wasn't broken, this would then work properly wasn't correct based on the results of this test. The Relationship is now working either way but the Ride Title isn't showing in this notification. It's important to have that in there because Ride Leaders lead many different rides so it is better for them to know which specific ride is referred to in this notification.

I'm so sorry that these issues keep persisting. This is so weird because the Ride Title functionality was working fine for a logged in user before and so I thought the Relationship failure was the only cause for why it didn't work for Guest users. If you did this submission as a Guest user, then the problem still persists with the correct Ride Title being included in this notification. If you did this submission as a logged in user, then this functionality has reverted back to not working at all.

#2608887

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry, that was due to as I removed all generic hidden fields from the form but actually we are using the generic hidden field "parent-post-title" to generate the custom place holder value.

So I added back the generic hidden field "parent-post-title" to the following form:
- hidden link

And we are using that generic hidden field "parent-post-title" value with the following code snippet "add-parent-post-title-email-notification":

add_filter('cred_subject_notification_codes', 'func_add_parent_post_title_to_email_notification', 10, 1);
add_filter('cred_body_notification_codes', 'func_add_parent_post_title_to_email_notification', 10, 1);
function func_add_parent_post_title_to_email_notification( $defaultPlaceHolders ) {
    if( !isset( $_REQUEST['parent-post-title'] )  ) {
      return $defaultPlaceHolders;
    }
    $newPlaceHolders = array( 
        '%%PARENT_POST_TITLE%%' => $_REQUEST['parent-post-title'],
    );
   
    return array_merge($defaultPlaceHolders, $newPlaceHolders );
}

Everything should we working now as expected with the new registration.