Preparing Gutenberg blocks for an email

  |   By  |  0 Comments

If you need to send a post or page out via email, you will need to parse blocks and shortcodes first.

I use MailChimp and the API doesn’t like audio and video tags. This function takes the post content and parses it creating buttons for playing audio and video.

In the switch($block[‘blockName’]) you can add other block types you may wish to process….

function prepare_post_for_email($post)
{
	
        $blocks=parse_blocks($post->post_content);
        /*********************************************
        * pasrse_blocks creates an array of blocks.
  	    * custom editor content is just one block
        *********************************************/
        $content='';
        if(!empty($blocks))
        {
            foreach($blocks AS $block)
            {
                
                switch($block['blockName'])
                {
                    case 'core/embed':
                       
                        $link=$block['attrs']['url'];
                        if(defined('CA_DEBUG'))church_admin_debug("Link $link ");
                        switch($block['attrs']['type'])
                        {
                            case 'audio':
                            case 'rich':
                                $label='Play audio';
                            break;
                            case 'video':
                                $label='Play video';
                            break;
                        }
                    	// Create a button in a table with the link and play audio or video
                        $content.='<table border="0" cellspacing="0" cellpadding="0"><tr><td align="center" style="border-radius: 3px;" bgcolor="#e9703e"><a href="'.esc_url($link).'" target="_blank" style="font-size: 16px; font-family: Helvetica, Arial, sans-serif; color: #ffffff; text-decoration: none; text-decoration: none;border-radius: 3px; padding: 12px 18px; border: 1px solid #e9703e; display: inline-block;">'.$label.'</a></td></tr></table>';
                    break;
                    default:
                        //any other block type, just grab the HTML
                        $content.=implode("\r\n",$block['innerContent']);
                    break;
                }
            }
        }else{ $content .= do_shortcode($post->post_content);}

	return $content;
}
name

ABOUT THE AUTHOR - ANDY MOYLE

Andy Moyle is a church leader and web developer. His biggest project is the Church Admin WordPress plugin and app. He also runs, mainly so he can eat pizza.