Skip to content

How to add post articles to your Gravity Forms email notifications

How to add post articles to your Gravity Forms email notifications. Ever wanted to add your latest blog articles and posts to an email autoresponder in Gravity forms? We’ll with the below handy little snippet, you can inject your 4 latest blog articles into any default or custom Gravity Forms notification.

How to add post articles to your Gravity Forms email notifications
How to add post articles to your Gravity Forms email notifications

Creating short-code

Add the below code snippet to your functions.php file. This snippet registers the shortcode in WordPress and creates the HTML markup that injects your latest posts into a table.

///////////////////////////////////////////////////////////////////////////
// Latest Posts in Gravity Forms email notifications - [email_latest_posts]
///////////////////////////////////////////////////////////////////////////

function custom_email_latest_posts_shortcode( $atts, $content = null ) {
 $content = do_shortcode( shortcode_unautop( $content ) );
 if ( '</p>' == substr( $content, 0, 4 )
  && '<p>' == substr( $content, strlen( $content ) - 3 ) )
  $content = substr( $content, 4, strlen( $content ) - 7 );

 $output = '';
 $args = array(
  'post_type' => 'post',
  'posts_per_page' => 4,
  'post_status' => 'publish',
  'fields' => 'ids'
 );
 $posts_query = new WP_Query($args);
 if ($posts_query->post_count > 0):
  ob_start();
  foreach($posts_query->posts as $post_id):
   ?>
   <table width="50%" align="left" cellpadding="10">
    <tbody>
    <tr>
     <td valign="top">
      <a href="<?php echo get_permalink($post_id); ?>" style="width: 100%; display: inline-block; height:200px; background: url(<?php echo get_the_post_thumbnail_url($post_id, 'medium'); ?>); background-size:cover!important; background-position: center center;"></a>
     </td>
    </tr>
    <tr>
     <td align="center" valign="top">
      <a style="font-size: 16px; font-family: inherit;  color: #ffffff; padding-bottom: 35px;  padding-top:10px; text-decoration: none; line-height: 23px;" href="<?php echo get_permalink($post_id); ?>">
       <p><?php echo get_the_title($post_id); ?></p>
      </a>
     </td>
    </tr>
    </tbody>
   </table>
  <?php
  endforeach;
  $output = ob_get_clean();
 endif;
 return $output;
}
add_shortcode( 'email_latest_posts', 'custom_email_latest_posts_shortcode' );

Add the shortcode

Now you’ve created the shortcode to output the 4 latest blog articles, all you need to do is add the following shortcode [email_latest_posts] to any of your Gravity Forms notification emails.

In the example above, I’ve created a custom HTML email, and added the shortcode to it, as below.

How to add post articles to your Gravity Forms email notifications

The result

The HTML email when sent will render the latest posts and add them into your email like below. Pretty simple hey? Just add the code snippet to your functions file and you can start adding the shortcode where ever you like! Make sure to leave a comment below and let us know what you think!

You can also get even more creative with some simple PHP and HTML to showcase all kinds of content in your shortcodes and emails. Try thinking about adding products from your WooCommerce or Easy Digital Downloads shop, or even the latest comments or reviews on products or blog posts. Skies the limit really.