Skip to content

Instantly share code, notes, and snippets.

@kylephillips
Last active June 18, 2020 16:04
Show Gist options
  • Save kylephillips/e3ff276a0355b2cb5d38 to your computer and use it in GitHub Desktop.
Save kylephillips/e3ff276a0355b2cb5d38 to your computer and use it in GitHub Desktop.
Email a list of favorites using Gravity Forms
/**
* Allow a user to email a list of favorites
* Place in functions.php and update to suit your specific form and needs
* @link https://www.gravityhelp.com/documentation/article/gform_notification/
*/
add_filter( 'gform_notification_1', 'email_favorites_list', 10, 3 );
function email_favorites_list($notification, $form, $entry)
{
// Get the user's favorites and create a list
$favorites = get_user_favorites();
if ( !$favorites ) return $notification;
$fq = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'post__in' => $favorites
));
if ( $fq->have_posts() ) :
// Styles should be set inline for display in email clients
$list = '<ul style="list-style-type:none;">';
while ( $fq->have_posts() ) : $fq->the_post();
$list .= '<li>';
if ( has_post_thumbnail() ) $list .= get_the_post_thumbnail(get_the_id());
$list .= '<h3>' . get_the_title() . '</h3>';
$list .= '<p>' . get_the_excerpt() . '</p>';
$list .= '</li>';
endwhile;
$list .= '</ul>';
else :
return $notification;
endif; wp_reset_postdata();
// Append the list to the notification message
$notification['message'] = $list;
// Change the to address to the user-specified email (this will change depending on your form)
$notification['to'] = $entry[1]; // Email Field
$notification['subject'] = __('A Favorites List from', 'textdomain') . ' ' . $entry[2]; // Name Field
return $notification;
}
@kylephillips
Copy link
Author

In the example above, the Gravity Form with an ID of "1" has 2 required fields: email & name. The email in this example is the 1st key in the $entry array, while the name field is the 2nd.
screen shot 2015-12-22 at 9 20 23 pm

@friiitz
Copy link

friiitz commented Sep 5, 2018

Has anyone tested this with or adapted it to work with Contact Form 7 and could share the effort?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment