PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

SOLUTION: Automatic email once post has been approved

  1. Being able to send a confirmation email to a non-WP registered (anonymous/guest) submitter, when their post has been approved, seems to be a very much requested feature by lots of users (including myself!).

    I can appreciate why it can't be done yet in Gravity Forms (although it should be in a future release), however I believe the code below would be a suitable, simple solution.

    The following assumes that you're already collecting an email address on the form, that its a required field and that the email address is being stored in a unique custom field.

    So simply add this code into your theme's functions.php file, replacing YOUR_CUSTOMFIELD_KEY with the name of the custom field that you're using to collect the email address.

    function email_members($post_ID)  {
    	global $post;
    	$email_to = get_post_meta($post->ID, 'YOUR_CUSTOMFIELD_KEY', true);
    	$headers = 'From: Name <name@name.com>' . \\"\r\n\\";
            $subject = "Your subject here";
    	$message = "Your message here";
        wp_mail($email_to, $subject, $message, $headers);
        return $post_ID;
    }
    
    add_action('publish_post', 'email_members');

    You can even do fancy things like displaying the permalink in the body of the message. Add this at the top of that code:

    $permalink = get_permalink( $post->ID );

    and then simply add $permalink into the $message variable e.g.

    $message = "Your message here " . $permalink . ". And then more text here!";

    It seems to be working for me and so in the spirit of open-source I just thought I'd share this with everyone else too!

    Duncan

    Posted 9 years ago on Thursday March 17, 2011 | Permalink
  2. Very cool Duncan. If I may make a suggestion, one thing that could also be done is after this executes it could add a new custom field to that post using a key designed so it's hidden and populate this with a value that basically says this code has executed and the email was sent.

    Then if for some reason the post is marked as a Draft again and then republished (for whatever reason) the user isn't necessarily emailed again for what may have just been a quick edit, etc. This way the email is only sent on initial publish.

    To hide a custom field from appearing in the list of custom fields for that post in the post edit screen you name the key with an underscore in front of it. For example: _emailstatus

    Although hiding this custom field from appearing is entirely up to you, obviously it may be beneficial to show this custom field in the post editor so you can see that the publish email was sent.

    Posted 9 years ago on Thursday March 17, 2011 | Permalink
  3. Hmmm, nice suggestion - certainly I could see instances of when that scenario could happen.

    Also to note, this code assumes that the form has also explicity stated that a submitters email address will be used to contact them - clearly there's no opt-out mechanism, so wouldn't want to fall foul of any legal obligations about reusing personal information.

    Might this code (or a derivative of), be easily included in a future release of GF? With a nice little backend interface to be able to control the messages etc? :-)

    D

    Posted 9 years ago on Thursday March 17, 2011 | Permalink
  4. Patag
    Member

    Looks nice, has this been implemented in the forms?
    If not, could you explain for lesser souls like me how to do the missing step of creating a custom field that will work with this? Where do I make that, in the dbase or..?

    Thanks

    Posted 9 years ago on Wednesday April 20, 2011 | Permalink
  5. Hi Patag,

    Assuming you are implementing this for posts created from a Gravity Form, you'll wanted to add a Post Custom Field from the Post Fields section on your form. Using the Post Custom Field you can store any user submitted value (or a number of system generated values) as a custom field of the post generated by the Gravity Form submission.

    Posted 9 years ago on Wednesday April 20, 2011 | Permalink
  6. Patag
    Member

    Thanks for your reply. I read it about 6 times, but it still did not get any clearer :)
    The form vs post vs custom post field and Post custom field is too confusing.

    Maybe I should have stressed more the 'how' question, as even if I would understand what you mean, I still would not know how to do it?

    Thanks in advance

    Posted 9 years ago on Wednesday April 20, 2011 | Permalink
  7. No problem. In Rnfsolutions original snippet, he is getting the user's email from a custom field. So what you'll need to do is add a "Custom Field" from the "Post Fields" field menu ( http://grab.by/9Vyb ) to your form. Then you'll update the "Custom Field Name" attribute of the Custom Field field (haha, hope that makes sense) to whatever you'd like to name your custom field ( http://grab.by/9Vyh ).

    Now back to Rnfsolutions snippet, you'd replace "YOUR_CUSTOMFIELD_KEY" in the code to the name of your custom field (in my example above, I named the custom field "user_email").

    Make sense? :)

    Posted 9 years ago on Wednesday April 20, 2011 | Permalink
  8. Is this possibly going to be included in a future release. Also, any consideration to it being triggered when a submitted post moves from draft to published?

    Posted 9 years ago on Saturday May 14, 2011 | Permalink
  9. nicole
    Member

    Has this been integrated into the plugin?
    I would like to do exactly this but since quite a while has passed since this post I am not sure if this function has been integrated or if I should try and follow these instructions?

    Posted 9 years ago on Wednesday July 20, 2011 | Permalink
  10. nicole
    Member

    I just tried to use this code but it did not work. Besides replacing YOUR_CUSTOMFIELD_KEY with the name of the custom field that you're using to collect the email address do you have to make any other changes to the code? For example post ID? Does it stay like that or do you put the ID of the post?

    Thanks in advance!

    Posted 9 years ago on Wednesday July 20, 2011 | Permalink
  11. Hi, great idea to do that this way. I'm using a custom post type for my form submissions and this way I'm able to test the field before I send the email message to that email address from my custom field.

    But there is a problem: I get the email address into my custom field and I used the same key name for the email function, but there is no email send after the post is gets published.
    Any idea what might be wrong?

    EDIT:
    I found the problem, because I'm using a custom post type (named "questions") I need to change the action:

    add_action('publish_questions', 'email_members');

    Great function even for non GF tasks :)

    Posted 9 years ago on Tuesday September 13, 2011 | Permalink

This topic has been resolved and has been closed to new replies.