Notify User When Submitted Post is Published
January 31, 2020: Fixed issue where email was not sent due to formatting issue in code.
This handy snippet allows you to send an email notification to the user who submitted a Gravity Forms generated post when that post is published.
This snippet assumes you have created a Post Custom Field which will store the submitting user’s email address. You would then specify the “Custom Field Name” you specified for the field as the $custom_field_name variable.
For example, assuming I had configured a Post Custom Field with the following settings:
I would set the $custom_field_name variable like so:
$custom_field_name = 'author_email';
Lastly, don’t forget to set your basic email settings!
$from_name = 'Your Name';
$from_email = 'your@email.com';
$subject = 'Your Subject Here';
$message = 'Your message here.';
This will only work for regular WordPress posts. If you’d like to apply to pages (or any custom post type), you’ll want to update the action from “publishpost” to “publish{your_post_type}”.
Is it only going to work for those who are logged in and registered member of the website?
No, it will send the email to whatever email address is saved in the “author_email” custom field.
It doesn’t work for me though
If you’re a Gravity Perks customer, we’ll be happy to provide additional support via the support form.
Hi David.
This snippet is not working and cause 500 error on my website.. I already added your snippets in functions.php and I add this code to theme functions.php too.
Regards, Vahid.
Hi Vahid, these troubleshooting tips might help: https://gravitywiz.com/documentation/snippet-troubleshooting/
Is there a way to make the message HTML? How do you include the post URL in the message?
Hi Jeph, you can include HTML in the message like so:
$message = '
Hello!
This is a message.
';
You can include a link to the post like so:
$message = 'View your post here: View Post';
Thanks David! I’ve been trying to play around adding other custom fields from the form, but I can’t quite figure it out? How would I include the post title and another field from the form?
You can get the post title like so:
$post_title = get_the_title( $post_id );
The entry bit is more complicated.
$entry = GFAPI::get_entry( get_post_meta( $post_id, '_gform-entry-id', true ) );
$my_value = $entry[3];
Replace the “3” with the field ID you want to retrieve the data from.