Create Your Own Custom Shortcode

You can extend the functionality of the MailPoet Designer by using custom shortcodes that inject content into your newsletters. These shortcodes can be used to customize both the body and the subject line of the newsletter.

Custom Shortcodes

There are two types of custom shortcodes available:

  • [custom:xxxx] outputs custom content. For example, the following code will render an HTML table inside your newsletter:
  • add_filter('mailpoet_newsletter_shortcode', 'mailpoet_custom_shortcode', 10, 5);
    
    function mailpoet_custom_shortcode($shortcode, $newsletter, $subscriber, $queue, $newsletter_body) {
      // always return the shortcode if it doesn't match your own!
      if ($shortcode !== '[custom:table]') return $shortcode; 
      
      $table =  "<table border=1>";
      $table .= "<thead><tr><th>Fruit Name</th><th>Fruit Color</th></tr></thead>";
      $table .= "<tbody><tr><td>Apple</td><td>Red</td></tr></tbody>";
      $table .= "<tbody><tr><td>Banana</td><td>Yellow</td></tr></tbody>";
      $table .= "</table>";
      
      return $table;
    }
  • [link:xxxx] outputs custom links. For example, the following code will add a referral link to your newsletter:
  • add_filter('mailpoet_newsletter_shortcode_link', 'mailpoet_custom_shortcode_referral_link', 10, 4);
    
    function mailpoet_custom_shortcode_referral_link($shortcode, $newsletter, $subscriber, $queue) {
      // always return the shortcode if it doesn't match your own!
      if ($shortcode !== '[link:referral]') return $shortcode;
      
      $referral = 'MailPoet';
      $referral_link = "<a href='http://example.com/?referral={$referral}'>Click here</a>";
      
      return $referral_link;
    }

Depending on the shortcode, you have access to a number of variables that can be helpful in creating your custom content:

  • (object) $subscriber ($subscriber->id, $subscriber->wp_user_id, $subscriber->email, $subscriber->first_name, $subscriber->last_name, $subscriber->created_at, ...)
  • (object) $newsletter ($newsletter->subject, $newsletter->type, $newsletter->created_at, ...)
  • (object) $queue ($queue->id, $queue->subscribers, ...). This variable is only available during sending.
  • (string) $newsletter_body

Example

Using the above code samples, you should see the following:

To visualize the output, you just need to click on the View in browser button:

Note: MailPoet does not provide support for code customization; therefore, whatever custom shortcode you create, your are responsible for troubleshooting any issues and supporting it!