Create a 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.

Just add these code snippets to the bottom of your theme's function.php file.

Need a developer to help you? We recommend those at Codeable (affiliate link).

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, 6);
    
    function mailpoet_custom_shortcode($shortcode, $newsletter, $subscriber, $queue, $newsletter_body, $arguments) {
      // 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 return a referral link URL to your newsletter:
  • add_filter('mailpoet_newsletter_shortcode_link', 'mailpoet_custom_shortcode_referral_link', 10, 5);
    
    function mailpoet_custom_shortcode_referral_link($shortcode, $newsletter, $subscriber, $queue, $arguments) {
      // always return the shortcode if it doesn't match your own!
      if ($shortcode !== '[link:referral]') return $shortcode;
      
      $referral = 'MailPoet';
      $referral_link = "http://example.com/?referral={$referral}";
      
      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->getId(), $subscriber->getWpUserId(), $subscriber->getEmail(), $subscriber->getFirstName(), $subscriber->getLastName()</span>, $subscriber->getCreatedAt(), &hellip;)
  • (object) $newsletter ($newsletter->getSubject(), $newsletter->getType(), $newsletter->getCreatedAt(), &hellip;)
  • (object) $queue ($queue->getId(), $queue->getSubscribers(), &hellip;). This variable is only available during sending.
  • (string) $newsletter_body
  • (array) $arguments. An associative array of arguments passed to the shortcode. Arguments can be passed as follows: [custom:xxxx arg1="val1" arg2="val2"]. If you use arguments, you may need to use a different check for a matching shortcode, e.g. if (strpos($shortcode, '[some:shortcode') !== 0) return $shortcode; That's because the shortcode string may contain arguments.

Example

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

To use the custom link shortcode, you should insert/edit a link and use the shortcode as the URL.

To visualize the output, you just need to click on the Preview button:

Note: DO NOT modify any of your theme files if you don't know what you are doing. MailPoet does not provide support for code customization; therefore, whatever custom shortcode you create, you are responsible for troubleshooting any issues and supporting it!

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.