Posted on by

Using a filter to change the user signup email

I sometimes get requests from people who want to change the email people get when they sign up depending on some value in the signup. For instance, if you have more than one signup form, and each one needs a different signup email.

In this tutorial, I’m going to show you a very basic application of this technique: stopping the email from getting sent if the signup is coming from a signup form where you don’t want the user to get an email.

First, you have to set up your signup form to record the name of the page it’s on (if you haven’t done this already). Create a new field, name it “Form Page” and make it a hidden field with a default value of “post->post_name” Click the signup checkbox so it will be included in the signup form.

That field (named ‘form_page’) will come in with the name of the page the form is on, and we will use that value to determine if the email will be sent or not. Let’s say the name of the page where you don’t want the email sent is called ‘signup-2’.

Now, to set up the filter. You need to put your filter in your theme functions file. Be aware that you should probably be doing this in a child theme or you will lose your customization.

The filter uses the Participants Database filter named “pdb-before_signup_thanks” and it provides you with the details of the outgoing email before it is sent. You also have access to the values in the new record. All you have to do is make your changes to those values and the outgoing email will be changed. We need to blank out the email address of the new registration so the email cannot be sent. The default name for the email field is ’email’ but that may be different in your configuration.

You can use this technique to make other changes, the principle is the same, but I just wanted to show this one simple thing so you get the general idea. Here is the filter you would use in your functions.php file:

add_filter('pdb-before_signup_thanks', 'my_modified_outgoing_email');

function my_modified_outgoing_email($outgoing_email) {
   // grab the values in the new submission
   $new_submission = $outgoing_email->participant_values;
   if ($new_submission['form_page'] == 'signup-2') {
      // blank out the recipient so the email won't be sent
      $outgoing_email->participant_values['email'] = '';
   }
}

(Note: when originally published, there was an error in the code; this has been fixed, and the explanation of how it works has been updated to reflect the change)

And that’s it! You will notice that unlike most WP filters, there is no return value. This is because we’re using a trick where the object that is provided to the filter function “refers” to the object in the plugin that holds the email values. If we change it in our filter function, it is also changed in the plugin…magically.

Have fun…

14 thoughts on “Using a filter to change the user signup email

  1. I am new on this plugin, whichs seems to can solve my task to establish club members. But I cannot find a function where I as administrator can send an email to some or all members.
    Hope it is possible. Please provide me with a link to read more about that. Thanks in advance.

    1. Hi Bent,

      To do something link this, you’ll need the Email Expansion Kit add-on I offer here on the site, It will give you extensive abilities to communicate with your members via email.

  2. Hi Roland,

    Thank you for this incredible plugin! Your dedication to not only offering this versatile plugin, but also offering your insights in response to visitor comments, is amazing.

    I have a quick question for you: I am interested in sending an email to the end-user when an email address is entered and record updated, but I only want it sent when new data for a particular set of fields is changed (e.g. details for an upcoming appointment). After reading through your posts, I came across two ways to do it: this current post, and this one: https://xnau.com/sending-an-email-when-a-record-is-updated/

    What I’ve done is created a hidden field that prints the page slug (as you suggested in the former article), and entered the custom code in functions.php to NOT send the email on that particular page. Then, I created a separate page that’s just for updating appointment details (using the groups filter). This way, whenever that record is saved, an email is sent out. However, I’ve been unsuccessful – the email is always sent, on both pages. Am I missing something? I can provide more info if I didn’t explain this well.

    Thanks in advance!

    1. Matt, hard to say what’s going wrong without seeing your filter. Your idea is sound, but there may be another way to do this using the Email Expansion Kit plugin I offer here. It has a functionality to discern if a particular field had its value changed and to send an email if it was changed.

      If you want to post your filter code, I can take a more detailed look at it.

      1. Thanks for the fast reply!

        I have this code on functions.php:

        add_filter(‘pdb-before_signup_thanks’, ‘my_modified_outgoing_email’);

        function my_modified_outgoing_email($outgoing_email) {
        // grab the values in the new submission
        $new_submission = $outgoing_email->participant_values;
        if ($new_submission[‘form_page’] == ‘edit-record’) {
        // blank out the recipient so the email won’t be sent
        $outgoing_email->participant_values[’email_address’] = ”;
        }
        }

        I then have my standard page (edit-record) that, when the record is saved via this page, an email should NOT be sent out. I have another page (modify-appointment) that, when the record is saved, an email should be sent out. The determinant is the ‘form_page’ hidden field with “post->post_name” in the default field on the backend. The reason for this design is, when accessing a record through edit-record, one can see all the tabs and fields within each tab, and because many fields can be edited, it’d get annoying for the person to receive an email about an appointment every time other tabs are being edited. On the ‘modify-appointment’ page I created, only the fields relating to the appointment are being shown, so upon a save, it would make sense for the person to be emailed with their appointment information.

        However, as previously mentioned, it’s seeming to be an all-or-nothing result. The code doesn’t seem to affect the outcome – only the checkbox on the backend enabling/disabling emails to be sent upon save. I’m thinking this is because of the “pdb-before_signup_thanks” call, since your article was written with using a signup field as the example, and this is only being used with the [record] shortcode with the ‘edit-record’ page slug. If you have any suggestions I’d really appreciate it!

        1. I see what’s happening: you need to blank out the email recipient, not the email in the participant_values property.

          $outgoing_email->recipient = '';

        2. That doesn’t seem to fix it. Could it be that the code is written for a new submission (signup form), and I’m looking to suppress the email being sent on an edit/update (record form)?

        3. I see, in that case you need to use the ‘pdb-before_submit_update’ filter. But it doesn’t work the same way at all. Let me give this some thought.

        4. OK, I’ve been answering too many support requests, wasn’t remembering what you were trying to do. We’re overthinking this. In your filter callback, you just need to test the value in your $post array first thing and just return without doing anything if you don’t want to send the email.

        5. Not a problem – I really appreciate your time. I also just realized that the value of ‘form_page’ isn’t updated with the new page name upon subsequent saves (e.g. it’s first saved on ‘edit-record’ and then later saved on ‘modify-appointment’, but the value of that field isn’t overridden by the plugin). Since this approach wouldn’t work, I’ll need to think of a more creative way to go about this. If something comes to mind, please let me know. Although I don’t think I need most of the functionality it has to offer, I might be better off with the paid email plugin.

          Thanks again!

        6. Well, there are probably a number of ways to do this but an easy one that would simply fix the immediate issue you’re having would be to place a hidden field for the ‘form_page’ in your form near the end. It will duplicate the hidden field of the same name, but since it’s later in the form it will override it…so something like this in your record form template:

          <input type="hidden" name="form_page" value="<?php echo $post->post_name ?>" />

        7. That actually worked perfectly, and also answered another question of mine for a different feature I needed. However, I am still a bit stuck in writing the code, since it doesn’t sound like it follows the framework of your other two articles for suppressing/changing emails on a create or update. Would you be able to point me in the right direction?

          Thank you again for your help, it means a ton.

        8. This is assuming you’re using the technique described in Sending an Email when a Record is Updated. In the code provided in that article, on the line where it checks is_admin(), you would check for your field value…something like this:

          if ( ! is_admin() || empty( $post['email'] || $post['form_page'] !== 'modify-appointment' ) ) {

        9. This didn’t seem to work, but strangely enough, I added:

          if ($form_page == “edit-record”) $email = $post[’email_address’];

          and then set ‘to’ in the array to $email, and that did work. Thank you so much for your help!

Leave a Reply

Your email address will not be published. Required fields are marked *

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.

14 thoughts on “Using a filter to change the user signup email

  1. I am new on this plugin, whichs seems to can solve my task to establish club members. But I cannot find a function where I as administrator can send an email to some or all members.
    Hope it is possible. Please provide me with a link to read more about that. Thanks in advance.

    1. Hi Bent,

      To do something link this, you’ll need the Email Expansion Kit add-on I offer here on the site, It will give you extensive abilities to communicate with your members via email.

  2. Hi Roland,

    Thank you for this incredible plugin! Your dedication to not only offering this versatile plugin, but also offering your insights in response to visitor comments, is amazing.

    I have a quick question for you: I am interested in sending an email to the end-user when an email address is entered and record updated, but I only want it sent when new data for a particular set of fields is changed (e.g. details for an upcoming appointment). After reading through your posts, I came across two ways to do it: this current post, and this one: https://xnau.com/sending-an-email-when-a-record-is-updated/

    What I’ve done is created a hidden field that prints the page slug (as you suggested in the former article), and entered the custom code in functions.php to NOT send the email on that particular page. Then, I created a separate page that’s just for updating appointment details (using the groups filter). This way, whenever that record is saved, an email is sent out. However, I’ve been unsuccessful – the email is always sent, on both pages. Am I missing something? I can provide more info if I didn’t explain this well.

    Thanks in advance!

    1. Matt, hard to say what’s going wrong without seeing your filter. Your idea is sound, but there may be another way to do this using the Email Expansion Kit plugin I offer here. It has a functionality to discern if a particular field had its value changed and to send an email if it was changed.

      If you want to post your filter code, I can take a more detailed look at it.

      1. Thanks for the fast reply!

        I have this code on functions.php:

        add_filter(‘pdb-before_signup_thanks’, ‘my_modified_outgoing_email’);

        function my_modified_outgoing_email($outgoing_email) {
        // grab the values in the new submission
        $new_submission = $outgoing_email->participant_values;
        if ($new_submission[‘form_page’] == ‘edit-record’) {
        // blank out the recipient so the email won’t be sent
        $outgoing_email->participant_values[’email_address’] = ”;
        }
        }

        I then have my standard page (edit-record) that, when the record is saved via this page, an email should NOT be sent out. I have another page (modify-appointment) that, when the record is saved, an email should be sent out. The determinant is the ‘form_page’ hidden field with “post->post_name” in the default field on the backend. The reason for this design is, when accessing a record through edit-record, one can see all the tabs and fields within each tab, and because many fields can be edited, it’d get annoying for the person to receive an email about an appointment every time other tabs are being edited. On the ‘modify-appointment’ page I created, only the fields relating to the appointment are being shown, so upon a save, it would make sense for the person to be emailed with their appointment information.

        However, as previously mentioned, it’s seeming to be an all-or-nothing result. The code doesn’t seem to affect the outcome – only the checkbox on the backend enabling/disabling emails to be sent upon save. I’m thinking this is because of the “pdb-before_signup_thanks” call, since your article was written with using a signup field as the example, and this is only being used with the [record] shortcode with the ‘edit-record’ page slug. If you have any suggestions I’d really appreciate it!

        1. I see what’s happening: you need to blank out the email recipient, not the email in the participant_values property.

          $outgoing_email->recipient = '';

        2. That doesn’t seem to fix it. Could it be that the code is written for a new submission (signup form), and I’m looking to suppress the email being sent on an edit/update (record form)?

        3. I see, in that case you need to use the ‘pdb-before_submit_update’ filter. But it doesn’t work the same way at all. Let me give this some thought.

        4. OK, I’ve been answering too many support requests, wasn’t remembering what you were trying to do. We’re overthinking this. In your filter callback, you just need to test the value in your $post array first thing and just return without doing anything if you don’t want to send the email.

        5. Not a problem – I really appreciate your time. I also just realized that the value of ‘form_page’ isn’t updated with the new page name upon subsequent saves (e.g. it’s first saved on ‘edit-record’ and then later saved on ‘modify-appointment’, but the value of that field isn’t overridden by the plugin). Since this approach wouldn’t work, I’ll need to think of a more creative way to go about this. If something comes to mind, please let me know. Although I don’t think I need most of the functionality it has to offer, I might be better off with the paid email plugin.

          Thanks again!

        6. Well, there are probably a number of ways to do this but an easy one that would simply fix the immediate issue you’re having would be to place a hidden field for the ‘form_page’ in your form near the end. It will duplicate the hidden field of the same name, but since it’s later in the form it will override it…so something like this in your record form template:

          <input type="hidden" name="form_page" value="<?php echo $post->post_name ?>" />

        7. That actually worked perfectly, and also answered another question of mine for a different feature I needed. However, I am still a bit stuck in writing the code, since it doesn’t sound like it follows the framework of your other two articles for suppressing/changing emails on a create or update. Would you be able to point me in the right direction?

          Thank you again for your help, it means a ton.

        8. This is assuming you’re using the technique described in Sending an Email when a Record is Updated. In the code provided in that article, on the line where it checks is_admin(), you would check for your field value…something like this:

          if ( ! is_admin() || empty( $post['email'] || $post['form_page'] !== 'modify-appointment' ) ) {

        9. This didn’t seem to work, but strangely enough, I added:

          if ($form_page == “edit-record”) $email = $post[’email_address’];

          and then set ‘to’ in the array to $email, and that did work. Thank you so much for your help!

Leave a Reply

Your email address will not be published. Required fields are marked *

Would you like to be notified of followup comments via e-mail? You can also subscribe without commenting.