WP Discourse: advanced custom filter for discourse_publish_format_html

For others, here’s how you’d modify my original block of code to work with the updated plugin, but this time with the desired oneboxed default :slight_smile::

// when publishing a post to Discourse
function cosmos_custom_publish_format_html( $output, $post_id ) {
	$post = get_post( $post_id );
	
	// if featured image is present, use it
	if ( has_post_thumbnail( $post_id ) ) {
		$image = "{thumbnail}<br><br>";
	} else {
		$image = "";
	}

    // if post is an event, get the custom field information
    if ( 'event' === $post->post_type) {
    	$startdate = get_post_meta($post->ID, '_event_start_date', true);
    	$starttime = get_post_meta($post->ID, '_event_start_time', true);
    	$enddate = get_post_meta($post->ID, '_event_end_date', true);
    	$endtime = get_post_meta($post->ID, '_event_end_time', true);
        $startdateformatted = date('j M Y', strtotime($startdate));
        $starttimeformatted = date('g:i a', strtotime($starttime));
        $enddateformatted = date('j M Y', strtotime($enddate));
        $endtimeformatted = date('g:i a', strtotime($endtime));
        $eventallday = get_post_meta($post->ID, '_event_all_day', true);
        $eventtimezone = get_post_meta($post->ID, '_event_timezone', true);
        $videoconferenceurl = get_post_meta($post->ID, 'Video Conference URL', true);
        
        if ($startdate == $enddate && $eventallday !== '1') {
          $eventinfo = "<strong>Date/Time:</strong> {$startdateformatted} @ {$starttimeformatted} - {$endtimeformatted} {$eventtimezone}";
        }
        
        if ($startdate !== $enddate && $eventallday !== '1') {
          $eventinfo = "<strong>Date/Time:</strong> {$startdateformatted} @ {$starttimeformatted} - {$enddateformatted} {$endtimeformatted} {$eventtimezone}";
        }
        
        if ($startdate == $enddate && $eventallday == '1') {
          $eventinfo = "<strong>Date/Time:</strong> {$startdateformatted} <i>All Day</i>";
        }
        
        if ($startdate !== $enddate && $eventallday == '1') {
          $eventinfo = "<strong>Date/Time:</strong> {$startdateformatted} - {$enddateformatted}";
        }
        
        if ($videoconferenceurl !== '') {
          $videoconferenceurlformatted = "<strong>Video Conference Link:</strong> {$videoconferenceurl}<br><br>";
        }
        
        if ($videoconferenceurl == '') {
          $videoconferenceurlformatted = "";
        }
                
        ob_start();
        ?>
	  
        <small>Originally published at: {blogurl}</small><br><br>
        <?php echo $image ?>
        <?php echo $eventinfo ?><br>
        <?php echo $videoconferenceurlformatted ?>
        {excerpt}
	  
        <?php 
        $output = ob_get_clean();
        
        return $output;
    }
    
    // if post is a group, get the custom field information
    if ( 'groups' === $post->post_type) {
    	$groupstartdate = get_post_meta($post->ID, 'wpcf-start-date', true);
    	$groupenddate = get_post_meta($post->ID, 'wpcf-end-date', true);
        $groupstartdateformatted = date('j M Y', strtotime($groupstartdate));
        $groupenddateformatted = date('j M Y', strtotime($groupenddate));
        $groupvideoconferenceurl = get_post_meta($post->ID, 'wpcf-video-conference-url', true);
        
        if ($groupenddate !== '') {
          $groupinfo = "<strong>Start Date:</strong> {$groupstartdateformatted}<br /><strong>End Date:</strong> {$groupenddateformatted}";
        }
        
        if ($groupenddate == '') {
          $groupinfo = "<strong>Start Date:</strong> {$groupstartdateformatted}<br /><strong>End Date:</strong> TBD";
        }
        
        if ($groupvideoconferenceurl !== '') {
          $groupvideoconferenceurlformatted = "<strong>Video Conference Link:</strong> {$groupvideoconferenceurl}<br><br>";
        }
        
        if ($groupvideoconferenceurl == '') {
          $groupvideoconferenceurlformatted = "";
        }
        	
        ob_start();
        ?>
	  
        <small>Originally published at: {blogurl}</small><br><br>
        <?php echo $image ?>
        <?php echo $groupinfo ?><br>
        <?php echo $groupvideoconferenceurlformatted ?>
        {excerpt}
        	  
        <?php
        $output = ob_get_clean();
	  
        return $output;
    }   
      
    // else return
    ob_start();
    the_permalink( $post_id );
	$output = ob_get_clean();
	return $output;
    
}
add_filter( 'discourse_publish_format_html', 'cosmos_custom_publish_format_html', 10, 2  );
4 Likes